Hello,

I tried to login to a router with a perl script. I opened a thread about this 2 days ago: http://www.perlmonks.org/?node_id=1126932

Here is the code for the script for logging into 1 router. This script works fine.

#!/usr/bin/perl -- use strict; use warnings; use WWW::Mechanize 1.73; my $browser = WWW::Mechanize->new( autocheck => 1 ); $browser -> agent("Mozilla/5.0"); $browser -> credentials('192.168.1.1:80','TEL','admin' => 'guess'); $browser -> timeout(10); $browser->show_progress( 1 ); $browser->get('http://192.168.1.1:80'); if( $browser->success ){ die " we logged in game over"; }

Now i have some problems if i modify the script for logging into 3 routers. I made a text file with the 3 IP's of the routers i want to login. The perl script is reading the IP's from the text file and fills the 3 IP's as strings in an array. I use a for loop for looping through the array and to setup a connection with the 3 routers. I have a 401 Authentication error when i run the script.

#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize 1.73; my $file = "output.txt"; open (FH, "< $file") or die "Can't open $file for read: $!"; my @lines; while (<FH>) { push (@lines, $_); } close FH or die "Cannot close $file: $!"; my $browser = WWW::Mechanize->new( autocheck => 1 ); $browser -> agent("Mozilla/5.0"); $browser -> timeout(10); $browser -> show_progress( 1 ); for (my $i=0; $i <= @lines; $i++) { my $url = "$lines[$i]"; $browser -> credentials("$url",'TEL','admin' => 'guess'); $browser->get("http://$url"); if( $browser->success ){ print "login to $url was successfull!!"; } }
Contect of textfile (output.txt): 192.168.1.1:80 192.168.2.1:80 192.168.3.1:80

This is the error message: ** GET http://192.168.1.1:80 ==> 401 Unauthorized Error GETing http://192.168.1.1:80: Unauthorized at routerlogin.pl line 21.

For some reason there is a problem when using variable $url in $browser -> credentials and $browser->get. I tried lots of things but can't figure it out. anyone a suggestion?


In reply to perl script logging into multiple routers by xubu83

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.