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

Discussion in 'Ruby' started by mrpink, Apr 19, 2007.

  1. mrpink

    mrpink Guest

    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')).body.scan(/^Ihre
    IP-Adresse ist.+/) { |ipadresse| puts
    ipadresse.sub(/^.+">/,'').sub(/<.+$/,'') }

    greets
    mrpink, Apr 19, 2007
    #1
    1. Advertising

  2. 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')).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
    James Edward Gray II, Apr 19, 2007
    #2
    1. Advertising

  3. James Edward Gray II wrote:
    > 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')).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



    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.
    Phillip Gawlowski, Apr 19, 2007
    #3
  4. mrpink

    mrpink Guest

    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
    mrpink, Apr 19, 2007
    #4
  5. On Apr 19, 2007, at 8:48 AM, Phillip Gawlowski wrote:

    > James Edward Gray II wrote:
    >> 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')).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

    >
    >
    > 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
    James Edward Gray II, Apr 19, 2007
    #5
  6. James Edward Gray 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://clothred.rubyforge.org

    Rule of Open-Source Programming #7:

    Release early, release often. Clean compilation is optional.
    Phillip Gawlowski, Apr 19, 2007
    #6
  7. mrpink

    John Joyce Guest

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

    > James Edward Gray 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!
    John Joyce, Apr 20, 2007
    #7
  8. mrpink

    mrpink Guest

    what you mean? :)
    mrpink, Apr 20, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Martelli

    "number-in-base" ``oneliner''

    Alex Martelli, Oct 29, 2004, in forum: Python
    Replies:
    21
    Views:
    646
    Andrea Griffini
    Nov 1, 2004
  2. Replies:
    2
    Views:
    271
    Steven Bethard
    Nov 1, 2004
  3. anton muhin
    Replies:
    1
    Views:
    278
    anton muhin
    Nov 1, 2004
  4. Jon Baer
    Replies:
    3
    Views:
    113
    Pit Capitain
    Jan 27, 2006
  5. John Carter
    Replies:
    3
    Views:
    457
    Robert Klemme
    Mar 30, 2007
Loading...

Share This Page