http://www.perlmonks.org?node_id=1093955

Tanto4545 has asked for the wisdom of the Perl Monks concerning the following question:

Im having an issue with a perl script im trying to run. The script runs through a list of routers and performs a "show interfaces descriptions" command at the command prompt and returns the value. For some reason i get the below error. I think it has something to do with Net::Telnet::Cisco but im not exactly sure. I can manually log into each router individiually but when i run the script, it fails.

print "Device list containte in the device.list file will be a CSV fi +le\n\n"; print "The device.list file will consist of only the device OS and cat +egory \n"; print "This script works on CISCO routers.\n\n"; print "Enter username:\n"; $username = <STDIN>; chomp $username; print "Enter password:\n"; $password = <STDIN>; chomp $password; print "Enter the command you want issued:\n"; $cmd = <STDIN> ; chomp $cmd; print "Enter the string you want to search for:\n"; $string = <STDIN>; chomp $string; print "\n\n\n"; #print "password is -$password-\n"; #print "username is -$username-\n"; #print "command is -$cmd-\n"; #print "search string is: -$string-\n"; use Net::Telnet::Cisco; open (DEVICE, 'device.list'); while (<DEVICE>) { chomp; $routerIP = $_; print "Router IP is $routerIP,"; my $session = Net::Telnet::Cisco->new(Host => "$routerIP", Input_log=> +"input.log", ); $session->login("$username", "$password"); my @output = $session->cmd("$cmd"); for $_ (@output) { #BRACKET A my $statline = $_; chomp $statline; if ($statline =~ /($string)/) { #Bracket B print "$statline"; } # end Bracket B } print "\n"; } exit;


after running the script i get this error:

bash-2.03$ perl CiscoCE_CMD_Query.pl Device list containte in the device.list file will be a CSV file The device.list file will consist of only the device OS and category This script works on CISCO routers. Enter username: XXXX Enter password: XXX! Enter the command you want issued: sh int des Enter the string you want to search for: RNC Router IP is ,login failed: access denied or bad username at CiscoCE_C +MD_Query.pl line 31

Replies are listed 'Best First'.
Re: Problems with Cisco Router Perl Query
by NetWallah (Canon) on Jul 17, 2014 at 03:03 UTC
    The answer to your speculation " For some reason i get the below error" is most likely in the "input.log" file that tracks the session.

    One big problem with your code is that it does no error checking.

    You should check for errors when you "open DEVICE", and check for a valid $session when it is opened.

    Most importantly, you should check for a non-zero return value from $session->login, and bail, if that fails.

    open (my $DEVICE, "<", 'device.list') or die "Could not open device li +st:$!"; while (<$DEVICE>) { chomp; my $routerIP = $_; print "Router IP is $routerIP,"; my $session = Net::Telnet::Cisco->new(Host => "$routerIP", Input_log +=>"input.log", ); if (! $session){ warn "Could not open session to $routerIP\n"; next; } if (! $session->login("$username", "$password") ){ warn "Could not login to $routerIP\n"; next; } my @output = $session->cmd("$cmd"); .... process ... }

            Profanity is the one language all programmers know best.

Re: Problems with Cisco Router Perl Query
by Theodore (Friar) on Jul 17, 2014 at 11:54 UTC
    Check that the $routerIP you are trying to use is actually a router ip. It looks to me that the first line of your input file is empty.

    Your code lacks proper error checking in many places.

Re: Problems with Cisco Router Perl Query
by tbone654 (Beadle) on Jul 19, 2014 at 04:24 UTC

    Try a different module Net::Telnet and a little more basic of a script just to test that it works first. I use this daily on Cisco MDS FC switches... Assuming different commands on routers would work also...

    #!/usr/bin/perl use strict; use warnings; use Net::Telnet; my $uri = shift @ARGV or die "IP address needed \n Usage: ./runssh 10. +10.10.77 outputfile"; my $a = shift @ARGV or die "output file needed \n usage: ./runssh 10.1 +0.10.77 outputfile"; my $telnet = new Net::Telnet (Timeout=>15 ,Input_log => $a ); $telnet->open($uri); $telnet->waitfor('/login: /i'); $telnet->print('OOQ91'); #put your login here $telnet->waitfor('/Password: /i'); $telnet->print('Password'); #put your password here print "Login is successful \n"; $telnet->prompt('/\w+# $/'); $telnet->waitfor($telnet->prompt); ### put some commands in here ### $telnet->cmd("terminal length 0"); $telnet->cmd("show interface brief"); $telnet->cmd("sh int desc"); $telnet->cmd("sh flogi data"); $telnet->cmd("sh zone act"); $telnet->waitfor($telnet->prompt); my $out = $telnet->print('exit'); print $out; my $fname = "received_data.txt"; open (my $fh, "<", $fname) or die "Can't open"; while (<$fh>) { print if /ip prefix-list/; }