Always Improving has asked for the wisdom of the Perl Monks concerning the following question:
#!/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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: accessing Cisco via SSH
by afresh1 (Hermit) on Oct 12, 2010 at 21:23 UTC | |
by Always Improving (Initiate) on Oct 13, 2010 at 00:51 UTC | |
by afresh1 (Hermit) on Oct 13, 2010 at 16:08 UTC | |
|
Re: accessing Cisco via SSH
by Anonymous Monk on Oct 13, 2010 at 10:51 UTC | |
by Always Improving (Initiate) on Oct 13, 2010 at 13:26 UTC | |
|
Re: accessing Cisco via SSH
by VinsWorldcom (Prior) on Oct 13, 2010 at 14:03 UTC |