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

Hi All

I have written a small script that would ideally login into a cisco router and run a command and display the output onto console but i have encountered with an error and not able to proceed further.

Initially i encountered with error as pattern match timed out.

Found an perl monk link where it was mentioned to add line "$session->print("\r"); # required to wakeup.".

After adding the above line i'm getting error as below even though username and password are correct

Error: login failed: access denied or bad username at core_switch.pl line 29

I'm suspecting its something related to prompt and delay in passing username and password, but not sure how to overcome that

Please post a sample code and point me in the right direction to avoid the error and connect Cisco device

Is there a way where whenever i execute this script, it should prompt me to input the username and password for switch

I would like to know both ways i.e. hard coding of username and password, and also when i execute the script it should prompt for username and password

My code

#!/usr/bin/perl use strict; use Net::Telnet; use Net::Telnet::Cisco; # Command line argument while executing the script if ($#ARGV < 0) { print "Please run the script with correct arguments\n\n"; print "Usage Details:perl core_switch.pl <Device IP>\n"; print "Ex: perl core_switch.pl 192.168.3.10\n"; exit; } my $ip = $ARGV[0]; print "=============================================================== +============\n"; print "Device IP:$ip\n"; #Connecting to Cisco Device my $session = Net::Telnet::Cisco->new(Host => $ip, Dump_Log => "dump.l +og"); $session->login('xxxxx','xxxxxxxxxx'); #Executing the commands print "=============================================================== +============\n"; print "CPU process history\n"; my @process_history= $session->cmd('Show process cpu history'); print "@process_history\n"; print "=============================================================== +============\n"; $session->close;

Replies are listed 'Best First'.
Re: Cannot connect to Cisco Device
by marto (Cardinal) on Apr 22, 2014 at 13:58 UTC

    In your code you have use Net::Telnet but never use this module. Net::Telnet::Cisco is 12 years old. Before using any module you should read the open bugs, for this module there are 11 years worth, some claim that it can't connect to certain Cisco hardware. Perhaps you're experiencing the same problem. A Super search will reveal lots of nodes on the subject of automating Cisco interfaces with perl. Using Net::Telnet with the appropriate waitfors should be all you need.

      Thank you Marto. However i was able to get it working after i captured the prompt of the device. I missed to read the complete documentation pertaining to Net::Telnet::Cisco