I searched around and have been unable to find any solution to the targeted issue I am having.
Purpose: This code is part of a larger script that polls our routers daily to help keep our DataBase up to date on our network connections and bandwidth availability.
Symptoms: The below stripped down code works fine with juniper routers and Cisco IOS based routers with minor tweaks the the command be passed. Unfortunately with the Cisco XR-IOS based routers, multiple command requests are failing after the first completes.
Other Notes: I have tried Net::SSH:Perl and Expect and they do not seem to be able to handle the manner in which the XR-IOS is passing the "security" banner. If I rework the code so that it starts a new ssh connection on each speed check then it gets through all the Database entries. the problem with that is for security reasons we lock the number of connections in a 5 minute period down, this results in the need for a "sleep" timer in the code which severely delays the completion of the script.
What am i looking for: I am hoping someone else has seen something like this and can offer assistance in getting the script to pass multiple commands in a single ssh connection to each router.
Below is the code and it's output. Any assistance or guidance is greatly appreciated.
#!/usr/bin/perl -w
use Net::Ping;
use Net::OpenSSH;
$username = "user";
$password = "pass";
@interface = ();
#Input data to fill above variables
get_router_data();
$listfiles = system("ls -al /root/scripts/logs/interfaces/");
$deletefiles = system("rm -rf /root/scripts/logs/interfaces/*");
print "$listfiles\n$deletefiles\n\n";
print "\nAttempting to connect to routers...\n";
$pingtest = Net::Ping->new("icmp");
cisco_test();
print "\n\nThe End....\n\n";
sub get_router_data {
$hostname = "router1";
$interface[0] = "Bundle-Ether11";
$interface[1] = "Bundle-Ether12";
$routercount = 2;
}
sub cisco_test {
if ($pingtest->ping($hostname, 2)) {
$ssh = Net::OpenSSH->new("$hostname", user => $username, passw
+ord => $password, master_stderr_discard => 1) || warn("\$!\n");
$testcount = 0;
while ($testcount < $routercount) {
$reportedspeed = 0;
print "\n\nPolling $hostname $interface[$testcount] ......
+... please wait\n";
open (LOGONE, ">/root/scripts/logs/interfaces/log.$hostnam
+e.$interface[$testcount].txt");
$out = $ssh -> capture ("show interfaces $interface[$testc
+ount] | include BW");
print "$out\n";
print LOGONE $out;
close (LOGONE);
open (LOGTWO, "/root/scripts/logs/interfaces/log.$hostname
+.$interface[$testcount].txt");
while ( <LOGTWO> ) {
@splitset = split (/\s+/, $_);
$speedcount = 0;
foreach $splittest (@splitset) {
if ($splittest =~ /BW/) {
$locationcount = $speedcount+1;
$reportedspeed = $splitset[$locationcount];
}
$speedcount++;
}
}
print "Reported Speed: $reportedspeed ...... \n";
close (LOGTWO);
$testcount++;
}
}
}
Output of the above script:
[scripts]# perl cisco_crs_int_check.pl
total 24
drwxr-xr-x. 2 root root 16384 Jan 30 10:50 .
drwxr-xr-x. 4 root root 4096 Jan 29 10:12 ..
-rw-r--r--. 1 root root 92 Jan 30 10:50 log.router1.Bundle-Ether11.
+txt
-rw-r--r--. 1 root root 0 Jan 30 10:50 log.router1.Bundle-Ether12.
+txt
0
0
Attempting to connect to routers...
Polling router1 Bundle-Ether11 ......... please wait
Thu Jan 30 15:51:37.001 UTC
MTU 9192 bytes, BW 240000000 Kbit (Max: 240000000 Kbit)
Reported Speed: 240000000 ......
Polling router1 Bundle-Ether12 ......... please wait
mux_client_request_session: read from master failed: Broken pipe
ssh_exchange_identification: read: Connection reset by peer
Reported Speed: 0 ......
The End....