Getting my IP address

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?

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?

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

Guy Decoux

ts decoux@moulon.inra.fr writes:

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?

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

···


Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
– Blair Houghton.

Ah, that answers my puzzle from a couple of days ago.

C:\WINDOWS>ruby -rsocket -e’puts IPSocket.getaddress(Socket.gethostname)’

… gives nothing / no error msg.

C:\WINDOWS>ruby -rsocket -e ‘puts IPSocket.getaddress(Socket.gethostname)’
213.249.173.103

… OK.

Difference: a space after -e

Space after -r is optional, apparently.

I’ll go and produce my “language stress graph” for this week :wink:

daz

···

“ts” decoux@moulon.inra.fr wrote:

svg% ruby -rsocket -e ‘p IPSocket.getaddress(Socket.gethostname)’
“138.102.114.196”
svg%

Guy Decoux

ruby -rwavy -e ‘puts Wavy.line(rand(80), rand(6), “.”)’

“daz” dooby@d10.karoo.co.uk wrote in message

C:\WINDOWS>ruby -rsocket -e’puts IPSocket.getaddress(Socket.gethostname)’

… gives nothing / no error msg.

C:\WINDOWS>ruby -rsocket -e ‘puts IPSocket.getaddress(Socket.gethostname)’
213.249.173.103

Difference: a space after -e

Yes, I stumbled on it too. One more thing one should be aware of: the single
quote
around the ruby script does not always work. In that case use double quote.
Like:

C:>ruby -ve ‘1.times{|i| puts i}’
ruby 1.8.0 (2003-06-23) [i386-mswin32]
-e:1: unterminated string meets end of file
‘i’ is not recognized as an internal or external command,
operable program or batch file.

C:>ruby -e “1.times{|i| puts i}”
0

Apparently if single quotes are used, DOS does not escape the special
characters
in this case the pipe ‘|’ symbol before ‘i’. But everything is fine within
the double quotes.
This is on WIN XP Pro.

Space after -r is optional, apparently.

Yes.

I’ll go and produce my “language stress graph” for this week :wink:

I bet there are only three points of stress :wink: (read the section “Dangerous
Ruby”
Pages [25,26] of Ruby Developer’s Guide for more details).

daz

– shanko

ruby -rwavy -e ‘puts Wavy.line(rand(80), rand(6), “.”)’
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What ?

How can I get, through Ruby, the IP addresses bound to each of the
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

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 :slight_smile:

daz

···

“Shashank Date” sdate@everestkc.net wrote:

ruby -rwavy -e ‘puts Wavy.line(rand(80), rand(6), “.”)’
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What ?

ts decoux@moulon.inra.fr wrote in message news:200307121340.h6CDeV804312@moulon.inra.fr

How can I get, through Ruby, the IP addresses bound to each of the
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

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%

If an interface has aliases, does it appear more than once?

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

Regards,

Brian.

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?

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

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

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

Guy Decoux

  * 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

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

Well, apparently Kame define getifaddrs()

Guy Decoux