Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

perl script logging into multiple routers

by xubu83 (Initiate)
on May 19, 2015 at 18:00 UTC ( [id://1127159]=perlquestion: print w/replies, xml ) Need Help??

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

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?

Replies are listed 'Best First'.
Re: perl cript logging into multiple routers
by GotToBTru (Prior) on May 19, 2015 at 18:06 UTC

    Might need to chomp your array of ips.

    Update: s/url/ip/

    Dum Spiro Spero

      Thanks!

      I added chomp(@lines); to the script and now it works fine. I didn't know perl adds a /n after each line in an array.

        Perl did not add them; they were in your file and it read them in.

        Dum Spiro Spero

        It doesn't.

        Perl does not, however, strip them out of the lines coming in from the file. Hence the existence of chomp (improved from its predecessor, chop).

        ++GotToBTru: Faster than me.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found