Hi All, I wrote a simple script that telnets into a list hosts and issues a command. Everything works fine, but the script fails whenever there is a failed login attempt. I'd like to create a if condition inside my subroutine and when there is a failed authentication, the program simply skips to the next host in the list. Does anyone know what cndition I should use for the if statement? Below is my code, thanks all.
#!/usr/bin/perl -w
#
#program to clear counters on all backhaul circuits
#
#
use Net::Telnet();
$telnet = Net::Telnet->new
(
Timeout => 90,
Prompt =>'/#/',
);
print "Enter your username: ";
$username = <STDIN>;
chomp($username);
# read in password from file
open (PASS, "passwd.txt"
or die("Error, file cannot be opened"
;
$pass = <PASS>;
close (PASS);
# read in routers from shell script 'drhost'
open (HOST, "/home/jschmoe/scripts/drhost |"
or die("Error, unable to open file"
;
# loop through each host while read in from file
while (defined($router = <HOST>) )
{
chomp ($router);
subtelnet ($router); # call subroutine to log into router
}
print "Done with script";
sub subtelnet
{
$telnet-> open ("$router"
;
$telnet-> login ($username, $pass);
$telnet-> cmd ("clear counter"
;
$telnet-> cmd ("yes"
;
$telnet-> cmd ("exit"
;
}
#!/usr/bin/perl -w
#
#program to clear counters on all backhaul circuits
#
#
use Net::Telnet();
$telnet = Net::Telnet->new
(
Timeout => 90,
Prompt =>'/#/',
);
print "Enter your username: ";
$username = <STDIN>;
chomp($username);
# read in password from file
open (PASS, "passwd.txt"
$pass = <PASS>;
close (PASS);
# read in routers from shell script 'drhost'
open (HOST, "/home/jschmoe/scripts/drhost |"
# loop through each host while read in from file
while (defined($router = <HOST>) )
{
chomp ($router);
subtelnet ($router); # call subroutine to log into router
}
print "Done with script";
sub subtelnet
{
$telnet-> open ("$router"
$telnet-> login ($username, $pass);
$telnet-> cmd ("clear counter"
$telnet-> cmd ("yes"
$telnet-> cmd ("exit"
}