Cisco : Script de sauvegarde automatique des configurations des switch
FIXME Il y a un petit bug dans ce script, si le serveur n'est pas atteignable par le switch l'erreur n'est pas remontée au script ...
#!/usr/bin/perl
use strict;
use Net::SSH::Perl;
use POSIX qw(strftime);
use Sys::Syslog qw( :DEFAULT setlogsock);
# Variables à configurer
my $user = "admin"; #switch username
my $pass = ""; #switch password
my $server = ""; #TFTP server
my @hosts = ("switch1","switch2");
sub logit {
my $programname;
my ($priority, $msg) = @_;
return 0 unless ($priority =~ /info|err|debug/);
setlogsock('unix');
openlog($programname, 'pid,cons', 'local1');
syslog($priority, $msg);
closelog();
return 1;
}
BEGIN {
logit('info', 'Cisco conf Backup Started');
}
my $now = strftime("%Y%m%d", localtime(time));
foreach my $host (@hosts) {
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user,$pass);
eval {
my ($stdout, $stderr, $exit) = $ssh->cmd('copy running-config tftp://'.$server.'/',"\n".$host."-".$now."-confg\n");
if($exit!='0') { logit('error', $host.': Config not saved ',$stderr); } else { logit('info', $host.': Config saved'); }
};
($@) ? ( logit('error', $@) ) : undef;
}
END {
logit('info', 'Cisco conf Backup Ended');
}