Getting my IP address

P

Philip Mak

Is there a piece of Ruby code somewhere that will tell me what my IP
address is, and will work on at least Linux, Windows and MacOS X?
 
T

ts

P> Is there a piece of Ruby code somewhere that will tell me what my IP
P> address is, and will work on at least Linux, Windows and MacOS X?

svg% ruby -rsocket -e 'p IPSocket.getaddress(Socket.gethostname)'
"138.102.114.196"
svg%


Guy Decoux
 
E

Eric Schwartz

ts said:
P> Is there a piece of Ruby code somewhere that will tell me what my IP
P> address is, and will work on at least Linux, Windows and MacOS X?

svg% ruby -rsocket -e 'p IPSocket.getaddress(Socket.gethostname)'
"138.102.114.196"

That only seems to work for the first IP address. It's not at all
uncommon for a box to have multiple IP addresses bound to one interface
(very common on a MacOS X box -- Rendezvous requires this), and it's
at least as common to have multiple interfaces on the same machine.

How this for a better question:

How can I get, through Ruby, the IP addresses bound to each of the
interfaces on this computer?

-=Eric
 
T

ts

E> How can I get, through Ruby, the IP addresses bound to each of the
E> interfaces on this computer?

You write an extension and you just hope that it work :)))

svg% ruby -ripo -e 'p IPSocket.names'
[["lo", "127.0.0.1"], ["eth0", "138.102.114.196"]]
svg%


Guy Decoux
 
D

daz

Shashank Date said:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What ?

You tried running a sig line ?? Some work, this won't.

If a 'wavy' module ever gets written, it will have your
reply as its documentation. Thanks.

I apologise if anyone's hopes were raised that it would
produce a language-stress graph. I'd just been looking
at some of Matz' slides which featured them as a concept
(of class Joke :)



daz
 
D

Daniel Berger

ts said:
E> How can I get, through Ruby, the IP addresses bound to each of the
E> interfaces on this computer?

You write an extension and you just hope that it work :)))

svg% ruby -ripo -e 'p IPSocket.names'
[["lo", "127.0.0.1"], ["eth0", "138.102.114.196"]]
svg%


Guy Decoux

I tried with sys-host. I don't think I got it right, though. Shall I
submit an RCR? Or does your code snippet mean that you've already got
some code to commit Guy?

Regards,

Dan
 
T

ts

D> I tried with sys-host. I don't think I got it right, though. Shall I
D> submit an RCR? Or does your code snippet mean that you've already got
D> some code to commit Guy?

The code snippet mean that actually I've problems.

For IPv4, you just create a socket and call ioctl(SIOCGIFCONF).

For IPv6, I dont know :

* some systems (Solaris, HP/UX, ...) has SIOCGLIFCONF
* for linux, probably you must use rtnetlink
* for Kame, I don't know, but perhaps it will work with SIOCGIFCONF

I don't know IPv6 and all implementations.

If someone want to look at this, it's best first to write an extension and
put it in RAA


Guy Decoux
 
T

ts

B> If that's a proposed extension, can I also suggest it would be useful to
B> retrieve netmasks as well as IPs. And the interface flags too ('up',
B> 'broadcast' etc)

It's easy to do when you have solved the problem for IPv6


Guy Decoux
 
T

ts

t> * some systems (Solaris, HP/UX, ...) has SIOCGLIFCONF

Well, at least this case is solved

nasun% ruby -vripo -e 'p IPSocket.names'
ruby 1.6.8 (2002-12-24) [sparc-solaris2.8]
[["lo0", "127.0.0.1"], ["eri0", "138.102.114.6"], ["lo0", "::1"], ["eri0", "fe80::203:baff:fe09:1469"]]
nasun%

Guy Decoux
 
T

ts

t> * for Kame, I don't know, but perhaps it will work with SIOCGIFCONF

Well, apparently Kame define getifaddrs()

Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top