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

Re: Obtaining my IP, strange results



T wrote:
... there is no reliable way to get my IP now.

I know it's not the answer to your specific question, but in terms of getting your IP address There's More Than One Way To Do It (tm) :-)

On my LinkSys DSL router I use a Perl script which uses LWP and HTML::TreeBuilder to get the status page from the router and grok out its IP address.


John Stumbles
......................................................................
#!/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 ;
    # 'dsl' is in my /etc/hosts, otherwise us IP address of your DSL
    # router (192.168. etc private IP address as seen from connected PC)
    $ua->credentials(
        'dsl:80', 		    # note 1
        'Linksys BEFW11S4',         # note 2
        'foo',                      # DSL login user name
        'bar'                       # DSL login passwd
    ) ;
    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 ..."
# 3	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
    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];
    my $ipadd_col  = ( $ipadd_row->look_down('_tag', 'td' ) )[1];
    #$ipadd_col->dump(); # debug
    print "\t", $ipadd_col->as_text, "\n";
    $tree->eof;
}

__END__
sub dump_contents {
    my $page_contents = shift or die "No contents returned from page\n";
    my $tree = HTML::TreeBuilder->new();
    $tree->parse($page_contents);
    $tree->dump();
    $tree->eof;
}



Reply to: