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

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.

  • Comment on Re^2: perl cript logging into multiple routers

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

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

    Dum Spiro Spero
Re^3: perl cript logging into multiple routers
by marinersk (Priest) on May 19, 2015 at 18:30 UTC

    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?

        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.

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

        -stevieb

        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?