[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to find out IP address used by router?



Ron Johnson wrote:

Most, if not every, home router made in the last 5 years has an
embedded web server and an internal IP address.

So if the external interface is down and thus can't get to ipchicken
or whatismyip, connect to that web server and it will tell you what
the external address is.

If you need to script that you can use Perl e.g.

#!/usr/bin/perl

use warnings;
use strict;
use HTML::TreeBuilder 2.97;
use LWP::UserAgent;

parse_contents (get_page());

sub get_page {
    my $ua = LWP::UserAgent->new ;
    $ua->credentials(
        'dsl:80',                   # note 1
        'Linksys BEFW11S4',         # note 2
        'username',                 # DSL login user name
        'passwd'                    # DSL login password
    ) ;
    my $request = HTTP::Request->new(
        GET => 'http://dsl/Status.htm'  # note 3
    ) ;
    my $response = $ua->request( $request ) ;
    unless($response->is_success) {
        warn "No response: ", $response->status_line, "\n";
        return;
    }
    return $response->content ;
}

# notes:
# 1     'dsl' in /etc/hosts is IP address (on private 192.168 network)
#       of my DSL box
# 2     is string in web login box
#           "Enter username and password for Linksys BEFW11S4 ..."
# 2     page containing public-facing IP address of DSL


sub parse_contents {
# this does the dirty work taking apart the HTML returned from the web page - this will be different for other routers
    my $page_contents = shift or die "No contents returned from page\n";
    my $tree = HTML::TreeBuilder->new();
    $tree->parse($page_contents);

    my $table = ( $tree->look_down('_tag','table') )[3];
    my $row  = ( $table->look_down('_tag', 'tr' ) )[10];
    my $inet_table  = ( $row->look_down('_tag', 'td')   )[0];
    my $ipadd_row  = ( $inet_table->look_down('_tag', 'tr' ) )[0];
    #...then do things with $col3...
    my $ipadd_col  = ( $ipadd_row->look_down('_tag', 'td' ) )[1];
    #$ipadd_col->dump();
    print "\t", $ipadd_col->as_text, "\n";
    $tree->eof;
}



Reply to: