views:

39

answers:

1

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)?

A: 

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.

averell