Skip to content

Instantly share code, notes, and snippets.

@Adadov
Created January 20, 2017 00:11
Show Gist options
  • Save Adadov/5534e9db76821f13e3b04e427dcb213d to your computer and use it in GitHub Desktop.
Save Adadov/5534e9db76821f13e3b04e427dcb213d to your computer and use it in GitHub Desktop.
Script de sauvegarde pour les configurations Cisco
#!/usr/bin/perl
use strict;
use Net::SSH::Perl;
use POSIX qw(strftime);
use Sys::Syslog qw(:DEFAULT setlogsock);
# Variables à configurer
my $user = "admin"; # Nom d'utilisateur
my $pass = ""; # Mot de passe
my $server = ""; # Serveur TFTP
my @hosts = ("switch1","switch2"); # Liste des switch
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('err', $host.': Config not saved ',$stderr); } else { logit('info', $host.': Config saved'); }
};
($@) ? ( logit('err', $@) ) : undef;
}
END {
logit('info', 'Cisco conf Backup Ended');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment