Getting IP addresses

M

Mark Van Holstyn

------=_Part_27073_27390581.1130310632973
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Does anyone know how to get an ip address from within ruby? I would like to
be able to grab both the ip of computer( the one assigned by my local
network ) and the ip to the outside world. Any help would be great.

Mark

--
Mark Van Holstyn
(e-mail address removed)
http://lotswholetime.com

------=_Part_27073_27390581.1130310632973--
 
Z

Zach Dennis

Mark said:
Does anyone know how to get an ip address from within ruby? I would like to
be able to grab both the ip of computer( the one assigned by my local
network ) and the ip to the outside world. Any help would be great.


irb -r socket
irb> Socket.getaddrinfo( Socket.gethostname, Socket::AF_INET )
=> [["AF_INET", 2, "lima", "10.0.1.44", 2, 2, 17]]

Zach
 
Z

Zach Dennis

Zach said:
Mark said:
Does anyone know how to get an ip address from within ruby? I would
like to
be able to grab both the ip of computer( the one assigned by my local
network ) and the ip to the outside world. Any help would be great.



irb -r socket
irb> Socket.getaddrinfo( Socket.gethostname, Socket::AF_INET )
=> [["AF_INET", 2, "lima", "10.0.1.44", 2, 2, 17]]

This answered your first question about your local system. The above
works on window, but on unix systems this will return whatever is setup
in your /etc/hosts file for your hostname. If you want to resolve IP by
dns, then look into using the Resolv library from the standard library...

To your second question. What is your ip to the outside world? Are you
referring to the ip address on your router? Or do you have multiple ip
addresses assigned to your local system?

Zach
 
D

Damphyr

Zach said:
Zach said:
Mark said:
Does anyone know how to get an ip address from within ruby? I would
like to
be able to grab both the ip of computer( the one assigned by my local
network ) and the ip to the outside world. Any help would be great.




irb -r socket
irb> Socket.getaddrinfo( Socket.gethostname, Socket::AF_INET )
=> [["AF_INET", 2, "lima", "10.0.1.44", 2, 2, 17]]


This answered your first question about your local system. The above
works on window, but on unix systems this will return whatever is setup
in your /etc/hosts file for your hostname. If you want to resolve IP by
dns, then look into using the Resolv library from the standard library...

To your second question. What is your ip to the outside world? Are you
referring to the ip address on your router? Or do you have multiple ip
addresses assigned to your local system?
I guess he's behind a NAT firewall and wants to know the IP address on
the provider side.
The way I used to resolve this was by using a service like
www.whatismyip.com or www.showmyip.com any one of the dynamic dns services.
Closest thing to a webservice I found is http://www.ippages.com/xml/
although the RSS format (http://www.ippages.com/rss.php) might be easier
to parse.
If anyone knows of a nice and free web service for this, let us know.
Cheers,
V.-
--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
Z

Zach Dennis

Damphyr said:
Zach said:
Zach said:
Mark Van Holstyn wrote:

Does anyone know how to get an ip address from within ruby? I would
like to
be able to grab both the ip of computer( the one assigned by my local
network ) and the ip to the outside world. Any help would be great.





irb -r socket
irb> Socket.getaddrinfo( Socket.gethostname, Socket::AF_INET )
=> [["AF_INET", 2, "lima", "10.0.1.44", 2, 2, 17]]



This answered your first question about your local system. The above
works on window, but on unix systems this will return whatever is
setup in your /etc/hosts file for your hostname. If you want to
resolve IP by dns, then look into using the Resolv library from the
standard library...

To your second question. What is your ip to the outside world? Are you
referring to the ip address on your router? Or do you have multiple ip
addresses assigned to your local system?
I guess he's behind a NAT firewall and wants to know the IP address on
the provider side.
The way I used to resolve this was by using a service like
www.whatismyip.com or www.showmyip.com any one of the dynamic dns services.
Closest thing to a webservice I found is http://www.ippages.com/xml/
although the RSS format (http://www.ippages.com/rss.php) might be easier
to parse.
If anyone knows of a nice and free web service for this, let us know.

I know you can use services like dyndns.org and no-ip.com to pick your
own domain name. You run a service on your local machine and it updates
the dns information. For example I can query 'myname.no-ip.com' using
the Resolv library to get my public ip address on the WAN side of my
home router/firewall...

Zach
 
P

Phil Hagelberg

require 'open-uri'

page = URI.parse("http://www.whatismyip.com")

html = page.read

ip = html.scan(/.*?([0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]
{1,3}).*/).flatten.first

Sweet--it's a bit more portable than just pulling it out of
system("ifconfig").

But why do you need it?

-Phil
 
Z

Zach Dennis

Joel said:
require 'open-uri'

page = URI.parse("http://www.whatismyip.com")

html = page.read

ip = html.scan(/.*?([0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]
{1,3}).*/).flatten.first

Beautiful! To go with what Joel did...

ip = html.scan( /(([0-9]{1,3}\.?){4})/ ).flatten.first

Zach
 

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
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top