Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Get External IP From Linksys Router

by meonkeys (Chaplain)
on Apr 28, 2004 at 15:03 UTC ( [id://348851]=sourcecode: print w/replies, xml ) Need Help??
Category: Networking Code
Author/Contact Info Adam Monsen <adamm@wazamatta.com>
http://adammonsen.com
Description:

This script can get the external (WAN) IP assigned to a Linksys BEFSR41/BEFSR11/BEFSRU31 router using the Web interface provided by the router. The external IP is printed to standard output.

requires WWW::Mechanize, HTML::Scrubber, HTML::Entities, and Regexp::Common.

A good alternative to this script is simply fetching http://checkip.dyndns.org/ and parsing that result.

#!/usr/bin/perl -w
use strict;

use WWW::Mechanize;
use HTML::Scrubber;
use HTML::Entities;
use Regexp::Common qw(net);

our $USER = 'admin';
our $PASS = 'secret';

my $mech = new WWW::Mechanize autocheck => 1;
my $scrubber = new HTML::Scrubber;

my $gateway_ip = '192.168.1.1:80';
$mech->credentials( 
  $gateway_ip, 'Linksys BEFSR41/BEFSR11/BEFSRU31', $USER, $PASS);
$mech->get("http://$gateway_ip/Status.htm");
my $content = $mech->content;
my ($html) = ($content =~ /WAN head-->(.*?)<!--WAN tail/);
$html = decode_entities($scrubber->scrub($html));
my ($ip) = ($html =~ /IP Address:($RE{net}{IPv4})/);
$ip or die "IP not found.";

print "$ip\n";
Replies are listed 'Best First'.
Re: Get External IP From Linksys Router
by TimToady (Parson) on Apr 29, 2004 at 00:31 UTC
    Or you can just use wget:
    #!/usr/bin/perl $status = `wget -O - --http-user=admin --http-passwd=secret http:/ +/192.168.1.1/Status.htm`; ($addr) = $status =~ /.*IP Address:.*?(\d+\.\d+\.\d+\.\d+)/; die "Can't find address" unless $addr; print "Address is $addr\n";
    Works for me, doesn't break when I upgrade Perl...
Re: Get External IP From Linksys Router
by Nkuvu (Priest) on Apr 28, 2004 at 23:43 UTC

    Another alternative to this script is the routerip.pl script created by Jerry LeVan. The advantage to his script is that it doesn't require any modules. The disadvantage is that it assumes curl is installed on the system. His regexen are probably more fragile, but the status page is pretty consistent.

    His page is here and the script is routerip.pl.

      For what it's worth, my latest favorite method of obtaining my public IP from the command line is using a DNS query from the command line.

      alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://348851]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (None)
    As of 2024-04-25 00:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found