Find external IP-address

F

Fred

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.
 
B

Ben Morrow

Fred said:
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
 
F

Fred

Ben said:
Fred said:
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
 
$

$_

Ben said:
Fred said:
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
 
R

Randal L. Schwartz

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,"
 
F

Fred

$_"@_.% said:
Ben said:
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
 
F

Fred

Randal L. Schwartz said:
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
 
C

Chris

Fred said:
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
 
B

Bill

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
 
F

Fred

Bill said:
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
 
F

Fred

Chris said:
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
 
C

Chris

Fred said:
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
 
P

pkent

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top