Plankton has asked for the wisdom of the Perl Monks concerning the following question:

Friends

I have an simple "Asante Cable/DSL Router" which gets its IP address from my ISP. I wanted to write a short script that uses WWW::Mechanize to login and scrape out what my IP address is. So here's the scripts I wrote (Removing my actual password of course) ...

#!/usr/bin/perl -w use strict; use WWW::Mechanize; use Data::Dumper; my $mech = WWW::Mechanize->new(); my $url = "http://192.168.123.254/begin.htm"; print "Getting $url ...\n"; $mech->get( $url ); print "Got!\n"; my $fields = { 'URL' => 'XXXXXXXX' }; #print Dumper ( $mech ); print "Submitting form ... \n"; my $r = $mech->submit_form( form_name => 'LOGIN', fields => $fields ); if ( $r->is_success ) { print "logged on!\n"; } else { print "Couldn't submit form:$!\n"; }
... when I run this I get ...
Couldn't submit form:Connection reset by peer
Anyone out there know why I might be getting this error and want I can do about it?

Replies are listed 'Best First'.
Re: Get IP address from my Asante Cable/DSL router
by blue_cowdawg (Monsignor) on Mar 12, 2006 at 23:09 UTC

    Hey Plankton!!
    Not necessarily the answer to your post, but here is my solution to essentially the same problem set you are dealing with. Perhaps you can adapt it accordingly?

    I opted not to use WWW::Mechanize but use a straight up LWP::UserAgent approach and an HTML parser to achieve the same result you are after.

    Just a thought...

Re: Get IP address from my Asante Cable/DSL router
by rafl (Friar) on Mar 12, 2006 at 23:24 UTC
Re: Get IP address from my Asante Cable/DSL router
by NetWallah (Canon) on Mar 13, 2006 at 06:47 UTC
    I overhauled blue_cowdawg's solution and came up with this, which works for my Windows + Netgear router. If you run Linux, you need to parse the output of 'ifconfig', instead of 'ipconfig'.

    Parsing the output of the Asante should be similar too.

    #!/usr/bin/perl -w ###################################################################### +## # Get External IP address of Netgear router ################################################# use strict; use warnings; use LWP::UserAgent; use Data::Dumper; #use HTML::TableContentParser; use Getopt::Long; my $host=""; my $user=""; my $password=""; my $addr = ""; my $port=80; my $Netgear_Page = "s_status.htm"; # Netgear Router status my $Linksys_Page = "Status_Router.asp"; my $result=GetOptions( "host=s" => \$host, "user=s" => \$user, "password=s" => \$password, "ipaddress=s" => \$addr, "port=i" => \$port ); my $ua=LWP::UserAgent -> new(); if ($host){ }else{ for (qx/ipconfig/){ m/^\s+Default Gateway\D+([\d|\.]+)/ and $host=$1 }; } barf_and_complain() unless ( $host and $user and $password ); my $host_settings=sprintf("%s:%d",$host,$port); my $url = sprintf("http://%s:%d/$Netgear_Page",$host,$port); my $req = HTTP::Request->new(GET => $url); $req->authorization_basic($user, $password); my $content = $ua->request($req)->as_string; ##print "----Content--\n$content-----\n"; while ($content=~m/<table.*?>(.+?)<\/table>/igs){ my $table = $1; ##print "----TABLE:$table\n\n"; $table =~m/<h\d>(.+?)<\/h\d>/igs and print "=====TABLEHEAD: $1\n"; for ("MAC Address", "IP Address", "Domain Name Server"){ $table =~m/<td.*?>$_.+?<\/td>\s*<td.*?>([\w|:|\.|<|>]+?)<\/td>/ and print "\t $_ $1\n"; } } sub barf_and_complain { printf "%s\n",qq( You must specify all options for this plugin to work: --host hostname or ip of router --user security user of router --ipaddress expected ip address --password login password for router the --port option is just that, an option and default +s to 80 ); exit(1); }
    Modifications made :
    • Try to guess internal IP of router - it should be your default gateway
    • Get rid of HTML::TableContentParser
    • Simplify parsing code
    • Use Userid instead of realm

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken