http://qs321.pair.com?node_id=864948

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

OK, so I wrote a telnet version and that works, but now we are migrating to SSh and I have (attempted) to modify it to figure out which to use, and be able to use SSH. So far, it works with telnet capable devices, buy still tries to SSH to them as well, and doesn't work with SSH devices. So, technically, it does nothing more than the original telnet script... Hmm.... Anyway, am I missing something small or am I completely off base and need to RTFM?
#!/usr/bin/perl -w ######################################################## # This script will tftp copies of the running config # # of a predetermined list of devices to a tftp server. # # The script will accept as input a text file which is # # a list of device IP addresses and produce a log file # # to list which devices were successfully backed-up. # # -- Written by Russell Gibbons # ######################################################## use Net::Telnet::Cisco; use Net::SSH::Perl; $ENV{'HOME'} = 'c:\strawberry'; $in_file = $ARGV[0]; # This is the filename of the input file with th +e IP addresses open (LOG, ">backup-log.txt") or die " cannot open log file!"; # log f +ile open (LIST, "<$in_file") or die " cannot open $in_file: $!"; # list +of IP addresses while (<LIST>) { chomp; push @ips, $_; # Populate array of IP addresses } close (LIST) or die " cannot close $in_file: $!"; $user = "********"; $pw = "********"; # Most special characters will require a pr +eceeding '\' $tftp_server = 'XX.XX.XX.XX'; # IP address of TFTP Server for(@ips) { $ssh = TRUE; eval { $cs = Net::Telnet::Cisco->new( Host =>$_, Timeout => 45); if ($cs->login( $user, $pw)) # Login { print "Opening Telnet session to $_\n"; $ssh = FALSE; # If telnet worked, no need to ss +h @hostline = $cs->cmd('sh run | inc hostname'); @hostline = split(/ /, $hostline[0]); chomp $hostline[1]; $hostname = $hostline[1]; print $hostname . "\n"; $cs->cmd("copy system:/running-config tftp://$tftp_server/$h +ostname-confg.txt\n\n\n"); print "sent CRT\n"; $cs->close; # Log out of device print LOG $hostname . ' - ' . $_ . ".\n"; } }; # End trying telnet if ($ssh) { eval { $cs = Net::SSH::Perl->new($_); if ($cs->login( $user, $pw)) # login { print "Opening SSH session to $_\n"; ### Seems to fail here, + or on the next line my(@hostline, $stderr, $exit) = $cs->cmd('sh run | inc hostname' +); @hostline = split(/ /, $hostline[0]); chomp $hostline[1]; $hostname = $hostline[1]; print $hostname . "\n"; $cs->cmd("copy system:/running-config tftp://$tftp_server/$h +ostname.txt\n\n\n"); print "sent CRT\n"; $cs->close; # Log out of device print LOG $hostname . ' - ' . $_ . ".\n"; } }; # End trying ssh } } close (LOG);