# Getting the IP from an external rescource in ruby

**URL:** <https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439>\
**Category:** Ruby\
**Created:** [April 19, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439 "2007-04-19T15:10:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 19, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/1 "2007-04-19T15:10:39Z")

</div>

Hi,  
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](http://www.wieistmeineip.de)’)).body.scan(/^Ihre  
IP-Adresse ist.+/) { |ipadresse| puts  
ipadresse.sub(/^.+"\>/,‘’).sub(/\<.+$/,‘’) }

greets

---

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 19, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/2 "2007-04-19T15:29:49Z")

</div>

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 🙂
> 
> require ‘net/http’
> 
> Net::HTTP.get\_response(URI.parse(‘http://  
> [www.wieistmeineip.de](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](http://www.wieistmeineip.de)”)’  
“212.19.62.76”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 19, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/3 "2007-04-19T15:48:56Z")

</div>

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](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](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://cynicalryan.110mb.com/)  
[http://clothred.rubyforge.org](http://clothred.rubyforge.org)

Rule of Open-Source Programming #37:

Duplicate effort is inevitable. Live with it.

---

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 19, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/4 "2007-04-19T16:00:57Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 19, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/5 "2007-04-19T16:02:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 19, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/6 "2007-04-19T16:14:58Z")

</div>

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 😉

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)  
[http://clothred.rubyforge.org](http://clothred.rubyforge.org)

Rule of Open-Source Programming #7:

Release early, release often. Clean compilation is optional.

---

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 20, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/7 "2007-04-20T14:10:48Z")

</div>

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 😉

One of these is a joke!

---

<div class="post-metadata">

**Author:** ![mrpink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mrpink](https://www.ruby-forum.com/u/mrpink)\
**Post date:** [April 20, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/getting-the-ip-from-an-external-rescource-in-ruby/92439/8 "2007-04-20T16:16:19Z")

</div>

what you mean? 🙂
