[src/oneliner] Getting the IP from an external rescource in ruby

J

James Edward Gray II

just for fun I wanna share this oneliner which prints you the
external IP on the screen :)

require 'net/http'

Net::HTTP.get_response(URI.parse('http://
www.wieistmeineip.de')).body.scan(/^Ihre IP-Adresse ist.+/) { |
ipadresse| puts ipadresse.sub(/^.+">/,'').sub(/<.+$/,'') }

Interesting. I expected my code to be equivalent, but it gives a
different answer:

$ ruby -r resolv -e 'p Resolv.getaddress("http://www.wieistmeineip.de")'
"212.19.62.76"

James Edward Gray II
 
P

Phillip Gawlowski

James said:
Interesting. I expected my code to be equivalent, but it gives a
different answer:

$ ruby -r resolv -e 'p Resolv.getaddress("http://www.wieistmeineip.de")'
"212.19.62.76"

James Edward Gray II


If I may translate: the German parts into English:


Net::HTTP.get_response(URI.parse('http://www.whatismyip.com')).body.scan(/^Your
WAN IP is.+/) { |ipadresse| puts ipadresse.sub(/^.+">/,'').sub(/<.+$/,'') }


The one liner resolves your external IP with the help of an external
service.

A hint for mrpink: German is not widely spoken beyond Germany, Austria
and Swiss, so it's a good idea to translate your output. ;)

--
Phillip "CynicalRyan" Gawlowski
http://cynicalryan.110mb.com/
http://clothred.rubyforge.org

Rule of Open-Source Programming #37:

Duplicate effort is inevitable. Live with it.
 
M

mrpink

lol my output prints a plain IP-adress on the screen and nothing else
and you're code is of course not working because you conducted my
regular expression with an output string muahhh :D
 
J

James Edward Gray II

If I may translate: the German parts into English:


Net::HTTP.get_response(URI.parse('http://
www.whatismyip.com')).body.scan(/^Your WAN IP is.+/) { |ipadresse|
puts ipadresse.sub(/^.+">/,'').sub(/<.+$/,'') }


The one liner resolves your external IP with the help of an
external service.

A hint for mrpink: German is not widely spoken beyond Germany,
Austria and Swiss, so it's a good idea to translate your output. ;)

Wow, I am dumb today. Thanks for helping me to actually read the
email I responded to.

James Edward Gray II
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top