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

I am trying to simply telnet to a switch and copy a new config file from a tftp server, then reboot the switch. I have read the documentation for Net::Telnet and Net::Telnet::Cisco. It doesn't seem to be that difficult in theory, but in practice I'm not getting the results I'm after. My output file shows that the commands were entered properly, but I don't see that my tftp server was accessed. I've performed these tasks manually to verify IP connectivity, and it worked fine. My code below currently is set for use of Net::Telnet::Cisco. But you'll also notice the pairs of 'waitfor' and 'print' from Net::Telnet. Neither configurations worked for me. The top of the script was verification that my match regexes were correct, as I've read that where most of these scripts fail is timeouts waiting to match a prompt. Regards, Scott
#!c:/Perl/bin/perl use strict; use warnings; use Net::Telnet::Cisco; #Switch#copy tftp start my $string1 = "Address or name of remote host []?"; my $string2 = "Source filename []?"; my $string3 = "Destination filename [startup-config]?"; #Accessing tftp://192.168.1.2/ap_config... #%Error opening tftp://192.168.1.2/ap_config (Timed out) my $string4 = "Switch#"; if ($string1 =~ /Address or name of remote host.*$/) { #print "String 1 matches.\n"; } else { #print "String 1 does not match.\n"; } if ($string2 =~ /Source filename.*$/) { #print "String 2 matches.\n"; } else { #print "String 2 does not match.\n"; } if ($string3 =~ /Destination filename.*$/) { #print "String 3 matches.\n"; } else { #print "String 3 does not match.\n"; } if ($string4 =~ /Switch#/) { #print "String 4 matches.\n"; } else { #print "String 4 does not match.\n"; } my $switch = "192.168.1.2"; my $tftp_server = "192.168.1.101"; my $config_file = "switch-confg"; my $dest_file = "startup-config"; my $user = "Cisco"; my $pass = "Cisco"; my $session = Net::Telnet::Cisco->new( Host => $switch, Input_log => "input.log", Output_log => "output.log", Timeout => 10); $session->waitfor_pause(0.6); $session->always_waitfor_prompt; my @out = $session->open($switch); # Wait for the username prompt and enter username #@out = $session->waitfor('/Username:.*$/'); #@out = $session->print($user); # Wait for the password prompt and enter the password @out = $session->waitfor('/Password:.*$/'); #print @out; @out = $session->print($pass); #print @out; @out = $session->cmd("copy tftp start\n$tftp_server\n$config_file\n$de +st_file\n"); #print @out; #@out = $session->waitfor('/Address or name of remote host.*$/'); #print @out; #@out = $session->print("$tftp_server\n"); #print @out; #@out = $session->waitfor('/Source filename.*$/'); #print @out; #@out = $session->print($config_file . "\n"); #print @out; #@out = $session->waitfor('/Destination filename.*$/'); #print @out; #@out = $session->print($dest_file . "\n"); #print @out; @out = $session->cmd("reload\n\n"); @out = $session->close;

Replies are listed 'Best First'.
Re: Cisco Switch
by karavelov (Monk) on Jul 19, 2008 at 15:55 UTC
    you could use code like that below.
    use Net::Telnet::Cisco; use strict my $host = '192.168.1.2'; my ($user,$pass,$enable_pass) = qw(username password enable_password); my $tftp_server = "192.168.1.101"; my $config_file = "switch-confg"; my $dest_file = "startup-config"; my $sess = Net::Telnet::Cisco->new(Host => '192.168.1.2'); $sess->login($user,$pass); $sess->enable($enable_pass); my @out = $sess->cmd("copy tftp start\n$tftp_server\n$config_file\n$de +st_file\n"); print @out; @out = $session->cmd("reload\n\n"); print @out; $sess->close;