Getting the IP from an external rescource in ruby

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

require ‘net/http’

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

greets

On Apr 19, 2007, at 8:10 AM, mrpink wrote:

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

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 G. II

James Edward G. II wrote:

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 G. 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. :wink:


Phillip “CynicalRyan” Gawlowski
http://cynicalryan.110mb.com/
http://clothred.rubyforge.org

Rule of Open-Source Programming #37:

Duplicate effort is inevitable. Live with it.

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

On Apr 19, 2007, at 8:48 AM, Phillip G. wrote:

Interesting. I expected my code to be equivalent, but it gives a
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. :wink:

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

James Edward G. II

James Edward G. II wrote:

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

No problem. I had similar trouble understand the email :wink:


Phillip “CynicalRyan” Gawlowski
http://cynicalryan.110mb.com/
http://clothred.rubyforge.org

Rule of Open-Source Programming #7:

Release early, release often. Clean compilation is optional.

On Apr 19, 2007, at 11:13 PM, Phillip G. wrote:

James Edward G. II wrote:

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

No problem. I had similar trouble understand the email :wink:

One of these is a joke!

what you mean? :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs