in reply to Re^3: perl cript logging into multiple routers
in thread perl script logging into multiple routers

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?

Replies are listed 'Best First'.
Re^5: perl cript logging into multiple routers
by stevieb (Canon) on May 19, 2015 at 21:29 UTC

    Please do not do mass edits or completely remove the text from a node, even if you create a new thread with the content (as you did in this case).

    As you can tell, my previous response has no context, and therefore makes absolutely no sense to future viewers of this node. I request you put back the original text, even though your question has already been answered on your new node.

    -stevieb

    EDIT: Parent post text appears to have been replaced, verbatim.

Re^5: perl cript logging into multiple routers
by stevieb (Canon) on May 19, 2015 at 21:00 UTC

    Try disabling 'autocheck' in the object instantiation call to new().

    -stevieb

Little issue perl script with WWW::Mechanize 1.73
by xubu83 (Initiate) on May 19, 2015 at 20:58 UTC

    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?

      I commented on your original post, but you removed the content from there to create this node. Try disabling "autocheck" in the object instantiation when calling new().

      my $browser = WWW::Mechanize->new( autocheck => 0 );

      -stevieb

      ps. As I noted in the other thread, please don't remove the contents of a post after it has been submitted, as it completely throws out of context all replies to it.

        I don't think papering over all errors is a good approach, especially for somebody new to WWW::Mechanize. I would instead suggest trapping the initial failure case within eval {...} and then skipping the device if the connection fails.

        That is where it originally was. OP was requested to put it back.

        -stevieb