Find external IP-address

Discussion in 'Perl Misc' started by Fred, Jan 11, 2004.

  1. Fred

    Fred Guest

    I want to keep a log of my IP-address. I installed the module
    Sys::HostIP on my Win98SP system, which is connected to the internet via
    cable-modem router:

    #!/usr/bin/perl -w

    use strict ;
    use Sys::HostIP ;

    my $ipaddr= hostip;
    print $ipaddr, "\n";

    This code works fine, however all I get is the internal (LAN-side)
    IP-address 192.168.1.100 which is useless. Is there a way to get the
    external (web-side) IP-address using Perl?
    I know that there are websites like
    http://home.jtan.com/~reader/my_tricky_remote_addr.cgi
    which return my IP-address, but I would prefer not to depend on a third
    party.

    --
    Fred
     
    Fred, Jan 11, 2004
    #1
    1. Advertising

  2. Fred

    Ben Morrow Guest

    Fred <> wrote:
    > I want to keep a log of my IP-address. I installed the module
    > Sys::HostIP on my Win98SP system, which is connected to the internet via
    > cable-modem router:
    >
    > #!/usr/bin/perl -w
    >
    > use strict ;
    > use Sys::HostIP ;
    >
    > my $ipaddr= hostip;
    > print $ipaddr, "\n";
    >
    > This code works fine, however all I get is the internal (LAN-side)
    > IP-address 192.168.1.100 which is useless. Is there a way to get the
    > external (web-side) IP-address using Perl?


    I don't really understand what you mean. Is your machine on a lan as
    well as the 'net, i.e. with this topology:

    { Lan }---[network card]-[your machine]-[cable-modem]---{ Internet }

    with 192.168.1.100 being the address of the network card?

    If this is the case, then, judging from the source of Sys::HostIP, I'd
    say your best bet is to parse the output of `ipconfig` yourself. You
    may also want to look at Win32::IPConfig and Win32::IPHelper.

    Ben

    --
    Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
    From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
    (Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
    /Alcestis/) [ flame, and falls out of sight. ]
     
    Ben Morrow, Jan 11, 2004
    #2
    1. Advertising

  3. Fred

    Fred Guest

    Ben Morrow wrote:
    >
    > Fred <> wrote:
    > > I want to keep a log of my IP-address. I installed the module
    > > Sys::HostIP on my Win98SP system, which is connected to the internet via
    > > cable-modem router:
    > >
    > > #!/usr/bin/perl -w
    > >
    > > use strict ;
    > > use Sys::HostIP ;
    > >
    > > my $ipaddr= hostip;
    > > print $ipaddr, "\n";
    > >
    > > This code works fine, however all I get is the internal (LAN-side)
    > > IP-address 192.168.1.100 which is useless. Is there a way to get the
    > > external (web-side) IP-address using Perl?

    >
    > I don't really understand what you mean. Is your machine on a lan as
    > well as the 'net, i.e. with this topology:
    >
    > { Lan }---[network card]-[your machine]-[cable-modem]---{ Internet }
    >
    > with 192.168.1.100 being the address of the network card?
    >
    > If this is the case, then, judging from the source of Sys::HostIP, I'd
    > say your best bet is to parse the output of `ipconfig` yourself. You
    > may also want to look at Win32::IPConfig and Win32::IPHelper.
    >
    > Ben
    >

    The topology is:
    {PC}--{network
    card}--192.168.1.100--{LAN}--{Router}--68.7.224.66--Internet
    What I want returned is 68.7.224.66 and not 192.168.1.100
    I will now look into Win32::IPConfig and Win32::IPHelper
    Thank you Ben,
    Fred
     
    Fred, Jan 11, 2004
    #3
  4. Fred

    $_@_.% Guest

    > Ben Morrow wrote:
    > >
    > > Fred <> wrote:
    > > > I want to keep a log of my IP-address. I installed the module
    > > > Sys::HostIP on my Win98SP system, which is connected to the internet via
    > > > cable-modem router:
    > > >
    > > > #!/usr/bin/perl -w
    > > >
    > > > use strict ;
    > > > use Sys::HostIP ;
    > > >
    > > > my $ipaddr= hostip;
    > > > print $ipaddr, "\n";
    > > >
    > > > This code works fine, however all I get is the internal (LAN-side)
    > > > IP-address 192.168.1.100 which is useless. Is there a way to get the
    > > > external (web-side) IP-address using Perl?

    > >
    > > I don't really understand what you mean. Is your machine on a lan as
    > > well as the 'net, i.e. with this topology:
    > >
    > > { Lan }---[network card]-[your machine]-[cable-modem]---{ Internet }
    > >
    > > with 192.168.1.100 being the address of the network card?
    > >
    > > If this is the case, then, judging from the source of Sys::HostIP, I'd
    > > say your best bet is to parse the output of `ipconfig` yourself. You
    > > may also want to look at Win32::IPConfig and Win32::IPHelper.
    > >
    > > Ben
    > >

    > The topology is:
    > {PC}--{network
    > card}--192.168.1.100--{LAN}--{Router}--68.7.224.66--Internet
    > What I want returned is 68.7.224.66 and not 192.168.1.100
    > I will now look into Win32::IPConfig and Win32::IPHelper
    > Thank you Ben,
    > Fred
    >

    dont use nat heh... why not just use the router to log incoming connections?
    use a syslog server
     
    $_@_.%, Jan 11, 2004
    #4
  5. >>>>> "Fred" == Fred <> writes:

    Fred> The topology is:
    Fred> {PC}--{network
    Fred> card}--192.168.1.100--{LAN}--{Router}--68.7.224.66--Internet
    Fred> What I want returned is 68.7.224.66 and not 192.168.1.100

    How would you expect it to know that?

    I'm curious. There's probably not a single 4 bytes on your
    box anywhere that have 68, 7, 224, and 66 in a sequence.
    Just a default route toward your router (probably 192.168.1.1).

    Maybe we should call the module ESP::psychic::IP::Diviner?

    print "Just another Perl hacker,"

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Jan 11, 2004
    #5
  6. Fred

    Fred Guest

    "$_"@_.% wrote:
    >
    > > Ben Morrow wrote:
    > > >
    > > > Fred <> wrote:
    > > > > I want to keep a log of my IP-address. I installed the module
    > > > > Sys::HostIP on my Win98SP system, which is connected to the internet via
    > > > > cable-modem router:
    > > > >
    > > > > #!/usr/bin/perl -w
    > > > >
    > > > > use strict ;
    > > > > use Sys::HostIP ;
    > > > >
    > > > > my $ipaddr= hostip;
    > > > > print $ipaddr, "\n";
    > > > >
    > > > > This code works fine, however all I get is the internal (LAN-side)
    > > > > IP-address 192.168.1.100 which is useless. Is there a way to get the
    > > > > external (web-side) IP-address using Perl?
    > > >
    > > > I don't really understand what you mean. Is your machine on a lan as
    > > > well as the 'net, i.e. with this topology:
    > > >
    > > > { Lan }---[network card]-[your machine]-[cable-modem]---{ Internet }
    > > >
    > > > with 192.168.1.100 being the address of the network card?
    > > >
    > > > If this is the case, then, judging from the source of Sys::HostIP, I'd
    > > > say your best bet is to parse the output of `ipconfig` yourself. You
    > > > may also want to look at Win32::IPConfig and Win32::IPHelper.
    > > >
    > > > Ben
    > > >

    > > The topology is:
    > > {PC}--{network
    > > card}--192.168.1.100--{LAN}--{Router}--68.7.224.66--Internet
    > > What I want returned is 68.7.224.66 and not 192.168.1.100
    > > I will now look into Win32::IPConfig and Win32::IPHelper
    > > Thank you Ben,
    > > Fred
    > >

    > dont use nat heh... why not just use the router to log incoming connections?
    > use a syslog server


    OK - that was a good hint.
    I can access the router with http://192.168.1.1/Status.htm,
    then save the page and extract the external IP from the saved HTML.
    Now I only have to get Perl to do that ...
    Fred
     
    Fred, Jan 11, 2004
    #6
  7. Fred

    Fred Guest

    "Randal L. Schwartz" wrote:
    >
    > >>>>> "Fred" == Fred <> writes:

    >
    > Fred> The topology is:
    > Fred> {PC}--{network
    > Fred> card}--192.168.1.100--{LAN}--{Router}--68.7.224.66--Internet
    > Fred> What I want returned is 68.7.224.66 and not 192.168.1.100
    >
    > How would you expect it to know that?
    >
    > I'm curious. There's probably not a single 4 bytes on your
    > box anywhere that have 68, 7, 224, and 66 in a sequence.
    > Just a default route toward your router (probably 192.168.1.1).
    >
    > Maybe we should call the module ESP::psychic::IP::Diviner?
    >
    > print "Just another Perl hacker,"
    > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095


    OK - I am now aware that 68.7.224.66 cannot be found on my PC and that
    there are probably only two ways to use Perl to get what I want:

    1. Use get the external IP from the router at
    http://192.168.1.1/Status.htm
    2. Or have an external Webpage show the IP to me.
    Fred
     
    Fred, Jan 11, 2004
    #7
  8. Fred

    Chris Guest

    Fred wrote:
    > "Randal L. Schwartz" wrote:
    >
    >>>>>>>"Fred" == Fred <> writes:

    >>
    >>Fred> The topology is:
    >>Fred> {PC}--{network
    >>Fred> card}--192.168.1.100--{LAN}--{Router}--68.7.224.66--Internet
    >>Fred> What I want returned is 68.7.224.66 and not 192.168.1.100
    >>
    >>How would you expect it to know that?
    >>
    >>I'm curious. There's probably not a single 4 bytes on your
    >>box anywhere that have 68, 7, 224, and 66 in a sequence.
    >>Just a default route toward your router (probably 192.168.1.1).
    >>
    >>Maybe we should call the module ESP::psychic::IP::Diviner?
    >>
    >>print "Just another Perl hacker,"
    >>Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

    >
    >
    > OK - I am now aware that 68.7.224.66 cannot be found on my PC and that
    > there are probably only two ways to use Perl to get what I want:
    >
    > 1. Use get the external IP from the router at
    > http://192.168.1.1/Status.htm
    > 2. Or have an external Webpage show the IP to me.


    Ummm, Ya-ah...! Either one of those is pretty easy to do in Perl, the
    external one is practically a piece of cake. I'm not sure what your
    hesitation is with using a "third party" so to speak. "I want to check
    the weather, but I don't want to use the internet to do it..." What's
    the story there? There's only two places, as you said, that are going
    to see your external address. That's the router and anything on the WAN
    side of it. So that's where you are going to have to go to get the
    information.

    I don't see what the problem is querying an external URL that returns
    REMOTE_ADDR? If you write your code in halfway decent style and the URL
    goes way there's go to be, gee-wiz, 20-30 other places you could chose
    from that report "Hey, your address is..." and repoint your URL...?

    Chris
    -----
    Chris Olive
    chris (-at-) technologEase (-dot-) com
    http://www.technologEase.com
    (pronounced "technologies")
     
    Chris, Jan 11, 2004
    #8
  9. Fred

    Bill Guest

    >
    > OK - I am now aware that 68.7.224.66 cannot be found on my PC and that
    > there are probably only two ways to use Perl to get what I want:
    >
    > 1. Use get the external IP from the router at
    > http://192.168.1.1/Status.htm



    yeah, that's the ticket.

    use LWP;
    use strict;
    use LWP::UserAgent;

    print "This is libwww-perl-$LWP::VERSION\n";
    my $r_url = 'http://192.168.1.1/Status.htm';

    my $ua = new LWP::UserAgent;
    $ua->agent("Godzilla/0.1 " . $ua->agent);
    my $req = new HTTP::Request(GET => $r_url);
    $req->content_type('application/x-www-form-urlencoded');

    # need auth???
    # $req->authorization_basic('user', 'password');

    print "Querying router...";
    my $res = $ua->request($req);
    # Check the outcome of the response
    $res->is_success or die "Cannot get response from router: $!";
    my $txt = $res->content;

    # now find the IP in $txt--use a regex I suppose


    HTH,

    Bill
     
    Bill, Jan 11, 2004
    #9
  10. Fred

    Fred Guest

    Bill wrote:
    >
    > >
    > > OK - I am now aware that 68.7.224.66 cannot be found on my PC and that
    > > there are probably only two ways to use Perl to get what I want:
    > >
    > > 1. Use get the external IP from the router at
    > > http://192.168.1.1/Status.htm

    >
    > yeah, that's the ticket.
    >
    > use LWP;
    > use strict;
    > use LWP::UserAgent;
    >
    > print "This is libwww-perl-$LWP::VERSION\n";
    > my $r_url = 'http://192.168.1.1/Status.htm';
    >
    > my $ua = new LWP::UserAgent;
    > $ua->agent("Godzilla/0.1 " . $ua->agent);
    > my $req = new HTTP::Request(GET => $r_url);
    > $req->content_type('application/x-www-form-urlencoded');
    >
    > # need auth???
    > # $req->authorization_basic('user', 'password');
    >
    > print "Querying router...";
    > my $res = $ua->request($req);
    > # Check the outcome of the response
    > $res->is_success or die "Cannot get response from router: $!";
    > my $txt = $res->content;
    >
    > # now find the IP in $txt--use a regex I suppose
    >
    > HTH,
    >
    > Bill


    Yes, that seems to work. And it is interesting that the router
    (LinkSys-befs41) requires a user/password when opening the main menu
    page, but *not* when opening Status.htm directly!
    Fred
     
    Fred, Jan 11, 2004
    #10
  11. Fred

    Fred Guest

    Chris wrote:
    >
    > Fred wrote:
    > > "Randal L. Schwartz" wrote:
    > >
    > >>>>>>>"Fred" == Fred <> writes:
    > >>
    > >>Fred> The topology is:
    > >>Fred> {PC}--{network
    > >>Fred> card}--192.168.1.100--{LAN}--{Router}--68.7.224.66--Internet
    > >>Fred> What I want returned is 68.7.224.66 and not 192.168.1.100
    > >>
    > >>How would you expect it to know that?
    > >>
    > >>I'm curious. There's probably not a single 4 bytes on your
    > >>box anywhere that have 68, 7, 224, and 66 in a sequence.
    > >>Just a default route toward your router (probably 192.168.1.1).
    > >>
    > >>Maybe we should call the module ESP::psychic::IP::Diviner?
    > >>
    > >>print "Just another Perl hacker,"
    > >>Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

    > >
    > >
    > > OK - I am now aware that 68.7.224.66 cannot be found on my PC and that
    > > there are probably only two ways to use Perl to get what I want:
    > >
    > > 1. Use get the external IP from the router at
    > > http://192.168.1.1/Status.htm
    > > 2. Or have an external Webpage show the IP to me.

    >
    > Ummm, Ya-ah...! Either one of those is pretty easy to do in Perl, the
    > external one is practically a piece of cake. I'm not sure what your
    > hesitation is with using a "third party" so to speak. "I want to check
    > the weather, but I don't want to use the internet to do it..." What's
    > the story there? There's only two places, as you said, that are going
    > to see your external address. That's the router and anything on the WAN
    > side of it. So that's where you are going to have to go to get the
    > information.
    >
    > I don't see what the problem is querying an external URL that returns
    > REMOTE_ADDR? If you write your code in halfway decent style and the URL
    > goes way there's go to be, gee-wiz, 20-30 other places you could chose
    > from that report "Hey, your address is..." and repoint your URL...?
    >
    > Chris


    (Not a Perl issue...) I hesitate to use a third party which returns
    REMOTE_ADDR because right now I know only one such site and if they shut
    down or change I will be stuck. I would like to add that feature to my
    own homepage, but members.cox.net does not allow CGI and I cannot
    imagine how to do this w/o CGI.
    --- Fred
     
    Fred, Jan 11, 2004
    #11
  12. Fred

    Chris Guest

    Fred wrote:
    > Bill wrote:
    >
    >>>OK - I am now aware that 68.7.224.66 cannot be found on my PC and that
    >>>there are probably only two ways to use Perl to get what I want:
    >>>
    >>>1. Use get the external IP from the router at
    >>>http://192.168.1.1/Status.htm

    >>
    >>yeah, that's the ticket.
    >>
    >>use LWP;
    >>use strict;
    >>use LWP::UserAgent;
    >>
    >>print "This is libwww-perl-$LWP::VERSION\n";
    >>my $r_url = 'http://192.168.1.1/Status.htm';
    >>
    >>my $ua = new LWP::UserAgent;
    >>$ua->agent("Godzilla/0.1 " . $ua->agent);
    >>my $req = new HTTP::Request(GET => $r_url);
    >>$req->content_type('application/x-www-form-urlencoded');
    >>
    >># need auth???
    >># $req->authorization_basic('user', 'password');
    >>
    >>print "Querying router...";
    >>my $res = $ua->request($req);
    >># Check the outcome of the response
    >>$res->is_success or die "Cannot get response from router: $!";
    >>my $txt = $res->content;
    >>
    >># now find the IP in $txt--use a regex I suppose
    >>
    >>HTH,
    >>
    >>Bill

    >
    >
    > Yes, that seems to work. And it is interesting that the router
    > (LinkSys-befs41) requires a user/password when opening the main menu
    > page, but *not* when opening Status.htm directly!
    > Fred


    They did that so you can do exactly what it is you are trying to do...

    Chris
    -----
    Chris Olive
    chris (-at-) technologEase (-dot-) com
    http://www.technologEase.com
    (pronounced "technlogies")
     
    Chris, Jan 12, 2004
    #12
  13. Fred

    pkent Guest

    In article <>, Fred <> wrote:

    > I want to keep a log of my IP-address. I installed the module
    > Sys::HostIP on my Win98SP system, which is connected to the internet via
    > cable-modem router:
    >
    > #!/usr/bin/perl -w
    >
    > use strict ;
    > use Sys::HostIP ;
    >
    > my $ipaddr= hostip;
    > print $ipaddr, "\n";
    >
    > This code works fine, however all I get is the internal (LAN-side)
    > IP-address 192.168.1.100 which is useless. Is there a way to get the
    > external (web-side) IP-address using Perl?



    If I've read your post correctly (and followups), and apologies if I
    haven't, it seems that you do not want the IP address of the one network
    interface in your Windows machine - what you want is the IP address of
    the internet-side interface in your cable modem, which is presumably a
    separate box.

    If that is the case then won't you have to ask the cable modem what its
    IP address is, because your PC only has the one interface in 192.168... ?

    P

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
     
    pkent, Jan 13, 2004
    #13
  14. Fred

    pkent Guest

    In article <>, Fred <> wrote:

    > OK - I am now aware that 68.7.224.66 cannot be found on my PC and that
    > there are probably only two ways to use Perl to get what I want:
    >
    > 1. Use get the external IP from the router at
    > http://192.168.1.1/Status.htm



    I'd

    use LWP::Simple;

    for that.

    P

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
     
    pkent, Jan 13, 2004
    #14
    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. Scott Allen
    Replies:
    8
    Views:
    10,946
    Scott Allen
    May 2, 2004
  2. Wybo Dekker
    Replies:
    1
    Views:
    366
    Yukihiro Matsumoto
    Nov 15, 2005
  3. vdvorkin
    Replies:
    0
    Views:
    414
    vdvorkin
    Feb 10, 2011
  4. vdvorkin
    Replies:
    3
    Views:
    816
    vdvorkin
    Feb 14, 2011
  5. Mellow Crow
    Replies:
    6
    Views:
    420
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page