Discussion:
How to get the IPs of all the interfaces in my host?
(too old to reply)
Iñaki Baz Castillo
2011-05-11 14:13:11 UTC
Permalink
Hi, basically I'm looking for a way to find all the IP's in the
network interfaces of my computer in a reliable way.

I know a "workaround":

--------------------------
UDPSocket.open {|s| s.connect "1.2.3.4", 80; s.addr }
---------------------------

=3D> ["AF_INET", 33564, "192.168.1.16", "192.168.1.16"]

But this method knows nothing about other IP's in the same interface,
neither allows me to discover other IP's in other interfaces with
different routes.

Is there something as "ifconfig" or "ip addr show" commands for Ruby?
Thanks a lot.


--=20
I=C3=B1aki Baz Castillo
<***@aliax.net>
Iñaki Baz Castillo
2011-05-11 15:22:17 UTC
Permalink
Post by Iñaki Baz Castillo
Hi, basically I'm looking for a way to find all the IP's in the
network interfaces of my computer in a reliable way.
--------------------------
UDPSocket.open {|s| s.connect "1.2.3.4", 80; s.addr }
---------------------------
=3D> ["AF_INET", 33564, "192.168.1.16", "192.168.1.16"]
But this method knows nothing about other IP's in the same interface,
neither allows me to discover other IP's in other interfaces with
different routes.
I got another way:

Socket::getaddrinfo(Socket.gethostname, "echo", Socket::AF_INET).map
{ |x| x[3] }

=3D> ["127.0.0.1", "127.0.0.1", "127.0.1.1", "127.0.1.1",
"192.168.1.16", "192.168.1.16"]

Not very cool, but...


PS: There is also a ruby-ifconfig library, not available via gem it seems.

--=20
I=C3=B1aki Baz Castillo
<***@aliax.net>
Eric Wong
2011-05-11 19:17:23 UTC
Permalink
Post by Iñaki Baz Castillo
Hi, basically I'm looking for a way to find all the IP's in the
network interfaces of my computer in a reliable way.
<snip>
Post by Iñaki Baz Castillo
Is there something as "ifconfig" or "ip addr show" commands for Ruby?
Thanks a lot.
in Ruby 1.9.2:

require 'socket'
Socket.ip_address_list
--
Eric Wong
Iñaki Baz Castillo
2011-05-11 19:33:09 UTC
Permalink
Post by Eric Wong
Post by Iñaki Baz Castillo
Is there something as "ifconfig" or "ip addr show" commands for Ruby?
Thanks a lot.
require 'socket'
Socket.ip_address_list
Amazing! I knew nothing about it :)

Thanks a lot.

--=20
I=C3=B1aki Baz Castillo
<***@aliax.net>

Loading...