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?


In reply to Little issue perl script with WWW::Mechanize 1.73 by xubu83
in thread 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.