Getting IP addresses

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
mvette13@gmail.com
http://lotswholetime.com

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

Socket.getaddrinfo( Socket.gethostname, Socket::AF_INET )

=> [["AF_INET", 2, "lima", "10.0.1.44", 2, 2, 17]]

Zach

Zach Dennis wrote:

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

Zach Dennis wrote:

Zach Dennis wrote:

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
> 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 What Is My IP | Whats My IP Address | GeoIP Location | Check IP Information | IP Tools although the RSS format (What Is My IP | Whats My IP Address | GeoIP Location | Check IP Information | IP Tools) 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.

Damphyr wrote:

Zach Dennis wrote:

Zach Dennis wrote:

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
> 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 What Is My IP | Whats My IP Address | GeoIP Location | Check IP Information | IP Tools although the RSS format (What Is My IP | Whats My IP Address | GeoIP Location | Check IP Information | IP Tools) 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

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

Tada!

-Joel

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

But why do you need it?

-Phil

···

On Thu, 2005-10-27 at 02:26 +0900, Joel Watson wrote:

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

Joel Watson wrote:

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