Hi,
I'm in the process of writing a perl script to telnet to cisco 857/870 series routers. The script itself works fine if the initial username and password are correct. Unfortunately, the routers have 2 possible sets of credentials and I would like to provide an alternate set to my script. I'm not particulalry good with perl, but thought there would be a way of trapping the script error output and provide a new username and password.
The current error returned from a failed login is:
Does anyone have any suggestions on how I might achieve trapping the error and replacing the username/password; or continuing with an alternate username/password? Script is below:
Thanks for your time,
Phil
I'm in the process of writing a perl script to telnet to cisco 857/870 series routers. The script itself works fine if the initial username and password are correct. Unfortunately, the routers have 2 possible sets of credentials and I would like to provide an alternate set to my script. I'm not particulalry good with perl, but thought there would be a way of trapping the script error output and provide a new username and password.
The current error returned from a failed login is:
login failed: access denied or bad username at ./telnet_cisco_arp.pl line 47
Does anyone have any suggestions on how I might achieve trapping the error and replacing the username/password; or continuing with an alternate username/password? Script is below:
Code:
#!/usr/bin/perl -w
use warnings;
use Net::Telnet::Cisco;
## BB and related test constants
#############################################################################
use constant GREEN => 'green';
use constant YELLOW => 'yellow';
use constant RED => 'red';
## BB Global variables
#############################################################################
my $prev_run_log = "$ENV{BBTMP}/$ENV{MACHINE}.ciscoarp.data";
my $bbtest = 'arp';
my $color = GREEN;
my $hostname;
my $username = "myusername1";
my $passwd = "mypassword1";
my $status = $bbtest . " OK";
my ($bbhost,$slaves);
my (@ips,@hosts);
## Main Program
#############################################################################
## TEST STUFF HERE
open (BBHOSTGREP, $ENV{BBHOME} . "/bin/bbhostgrep " . "snmparp" . " |") or die;
while (<BBHOSTGREP>) {
my ($ip,$host);
($ip,$host) = split ' ';
print "DEBUG Adding $host $ip to server list\n";
push @ips, $ip;
push @hosts, $host;
}
close BBHOSTGREP;
foreach $ip (@ips) {
$bbhost = $ip;
$hostname = $host;
print "DEBUG Connecting to $bbhost\n";
my $session = Net::Telnet::Cisco->new(Host => $bbhost,
Errmode => sub { warn $_[0] });
$session->login($username, $passwd);
@lines = $session->cmd("show arp");
print @lines;
my $DATA = "@lines";
## Send to Hobbit
######################################################################
my $report_date = `/bin/date`;
chomp($report_date);
$host=shift(@hosts);
print "DEBUG Sending $host DATA to Xymon\n";
system("$ENV{BB} $ENV{BBDISP} 'status $host.$bbtest $color $report_date
- $status\n\n$DATA'\n");
}
Thanks for your time,
Phil