Update:
Thanks for the suggestions, they got the old grey matter moving. I tried a few attempts with both suggestions (Expect & CLI). Neither proved fruitful immediately. However, as it so happens all to often for me, something in the examples of the other modules gave me a potential modification to the original approach.
I'm putting the code and output below for the benefit of others in case they are attempting something simular with the XR-IOS boxes from Cisco.
What I ended up doing was removing the loop for each command after the ssh connection was established. I instead added a check of the interface description followed by the original command. This was looped to create a single variable containing all the interfaces to be passed after the connection was made. It'may not be the prettiest but it is effective. I just now have to apply it to the larger script I am attempting to update.
Again, tahnks for the suggestions and making me look at this from a different perspective.
#!/usr/bin/perl -w
use Net::Ping;
use Net::OpenSSH;
$username = "user";
$password = "password";
@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";
$cmd = "";
foreach $intname (@interface) {
$cmd = "$cmd\nshow interfaces $intname | include Bundle\nshow
+interfaces $intname | include BW\n";
}
}
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;
print "\n\nPolling $hostname ......... please wait\n";
open (LOGONE, ">/root/scripts/logs/interfaces/log.$hostname.tx
+t");
$out = $ssh -> capture ("$cmd");
print "$out\n";
print LOGONE $out;
close (LOGONE);
open (LOGTWO, "/root/scripts/logs/interfaces/log.$hostname.txt
+");
$interfacecount = 0;
@interfacenamerecord = ();
@interfacespeedrecord = ();
while ( <LOGTWO> ) {
@splitset = split (/\s+/, $_);
$speedcount = 0;
foreach $splittest (@splitset) {
if ($splittest =~ /Bundle/) {
$interfacenamerecord[$interfacecount] = $splitset[
+0];
}
if ($splittest =~ /BW/) {
$locationcount = $speedcount+1;
$interfacespeedrecord[$interfacecount] = $splitset
+[$locationcount];
$interfacecount++;
}
$speedcount++;
}
}
$printcount = 0;
$intholder = "";
foreach $intholder (@interfacenamerecord) {
print "Interface $interfacenamerecord[$printcount] reporte
+d speed is $interfacespeedrecord[$printcount]\n";
$printcount++;
}
close (LOGTWO);
$testcount++;
}
}
Evaluation output
[scripts]# perl cisco_crs_int_check.pl
total 24
drwxr-xr-x. 2 root root 16384 Jan 30 14:20 .
drwxr-xr-x. 4 root root 4096 Jan 29 10:12 ..
-rw-r--r--. 1 root root 326 Jan 30 14:20 log.router1.txt
0
0
Attempting to connect to routers...
Polling router1 ......... please wait
Thu Jan 30 19:26:37.629 UTC
Bundle-Ether11 is up, line protocol is up
Thu Jan 30 19:26:37.975 UTC
MTU 9192 bytes, BW 240000000 Kbit (Max: 240000000 Kbit)
Thu Jan 30 19:26:38.268 UTC
Bundle-Ether12 is up, line protocol is up
Thu Jan 30 19:26:38.545 UTC
MTU 9192 bytes, BW 340000000 Kbit (Max: 340000000 Kbit)
Interface Bundle-Ether11 reported speed is 240000000
Interface Bundle-Ether12 reported speed is 340000000
The End....
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.