Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Find external IP-address

Reply
Thread Tools

Find external IP-address

 
 
Fred
Guest
Posts: n/a
 
      01-11-2004
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
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      01-11-2004

Fred <(E-Mail Removed)> 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. ] http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      01-11-2004
Ben Morrow wrote:
>
> Fred <(E-Mail Removed)> 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
 
Reply With Quote
 
$_@_.%
Guest
Posts: n/a
 
      01-11-2004
> Ben Morrow wrote:
> >
> > Fred <(E-Mail Removed)> 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
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      01-11-2004
>>>>> "Fred" == Fred <(E-Mail Removed)> 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:sychic::IP:iviner?

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <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!
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      01-11-2004
"$_"@_.% wrote:
>
> > Ben Morrow wrote:
> > >
> > > Fred <(E-Mail Removed)> 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
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      01-11-2004
"Randal L. Schwartz" wrote:
>
> >>>>> "Fred" == Fred <(E-Mail Removed)> 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:sychic::IP:iviner?
>
> 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
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      01-11-2004
Fred wrote:
> "Randal L. Schwartz" wrote:
>
>>>>>>>"Fred" == Fred <(E-Mail Removed)> 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:sychic::IP:iviner?
>>
>>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")

 
Reply With Quote
 
Bill
Guest
Posts: n/a
 
      01-11-2004
>
> 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

 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      01-11-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM
unresolved external symbol/using an external dll Scott Allen C++ 8 05-02-2004 06:11 PM



Advertisments