in reply to perl script logging into multiple routers

Might need to chomp your array of ips.

Update: s/url/ip/

Dum Spiro Spero
  • Comment on Re: perl cript logging into multiple routers

Replies are listed 'Best First'.
Re^2: perl cript logging into multiple routers
by xubu83 (Initiate) on May 19, 2015 at 18:27 UTC

    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.

        This script logs into to 3 routers in my network. Th script is working fine if all the 3 IP's (routers) are up and accessible. But if one of the routers is not accessible, then the for loop finish and the script ends, instead of hopping onto the next IP in the array.

        I show you the output when i run the script with router 2 (192.168.2.1)disabled:

        ** GET http://192.168.1.1:80 ==> 401 Unauthorized (1s)

        ** GET http://192.168.1.1:80 ==> 200 OK

        ** GET http://192.168.2.1:80 ==> 404 Not Found (1s)

        Error GETing http://192.168.2.1:80: Not Found at routerlogin.pl line 20.

        login to 192.168.1.1:80 was successfull!!

        The script should connect to router 3 (192.168.3.1) but neglects it after receiving a 404 <Not Found! I tried to edit the script like this but that doesn't work.

        #!/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: $!"; chomp(@lines); for (my $i=0; $i <= @lines; $i++) { my $url = "$lines[$i]"; my $browser = WWW::Mechanize->new( autocheck => 1 ); $browser -> agent("Mozilla/5.0"); $browser -> timeout(10); $browser -> show_progress( 1 ); $browser -> credentials("$url",'TEL','admin' => 'guess'); $browser -> get("http://$url"); if($browser -> success){ print "login to $url was successfull!!"; } else{ print "login to $url was NOT successfull!!"; } }

        After receiving the 404 <Not Found i want the script to print the text in the else clause: "login to $url was NOT successfull!!" and go further.

        What am i missing here?