开发者

Can I make ruby send network traffic over a specific iface?

I am writing a script that grabs the external IP address along with some other information about each iface. I can't figure out how to make ruby send traffic over a specific iface(eth0 and wlan0 for example). I am currently using the open开发者_开发百科-uri library to open http://whatismyip.org and read it into a variable.

def get_external_ip
  begin
    open("http://whatismyip.org") { |f| return f.read }
  rescue
    return "none"
  end
end

Is there any way to specify this at runtime of the script or from within the script itself(ideally)?


You could check this tutorial on Ruby Socket programming. But I doubt that it can be done on a high level like open. The interface will be selected normally from the routing table, depending on the destination IP - so maybe it will be easier to fix/adjust your routing table instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜