Created
May 6, 2010 04:24
-
-
Save dynax60/391775 to your computer and use it in GitHub Desktop.
Cisco backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use Data::Dumper; | |
use YAML; | |
use FindBin qw/$Bin/; | |
use Net::Telnet::Cisco; | |
++$|; | |
umask 012; | |
$0 =~ s{.*/}{}; | |
$ENV{PATH} = '/bin:/usr/bin:/sbin:/usr/sbin'; | |
(my $cfg_file_default = "$Bin/$0.yml") =~ s/\.pl//; | |
my $cfg_file = shift || $cfg_file_default; | |
die "Usage: $0 [config.yml]\n" unless -r $cfg_file; | |
my $cfg = YAML::LoadFile($cfg_file); | |
foreach ( @{ $cfg->{hosts} } ) { | |
my $cfg_host_file = $cfg->{tftp_backup_dir}.'/'.$_; | |
qx{ touch $cfg_host_file }; | |
&backup( $_ ); | |
&time_cutter( $cfg_host_file ); | |
if (-r $cfg_host_file) { | |
print qx{ $cfg->{rcs_script} $cfg_host_file 2>&1 }; | |
} | |
} | |
sub backup { | |
my $host = shift; | |
my $t = new Net::Telnet::Cisco( | |
Host => $host, | |
Timeout => $cfg->{timeout}, | |
Prompt => '/[>#]\s*$/' | |
# Dump_Log => '/dev/stdout', Input_log => '/dev/stdout', | |
); | |
$t->login($cfg->{user}, $cfg->{password}); | |
#$t->cmd('terminal length 0'); | |
sleep 4; | |
die "Cannot enable: $t->errmsg" unless $t->enable($cfg->{en_password}); | |
$t->cmd("write net"); | |
} | |
sub time_cutter { | |
my $file = shift; | |
open R, "< $file" || die; undef $/; $_ = <R>; close R; | |
s/(^: Written by .*?) at \S+ [A-Z]{3}/$1 at/gm; | |
open W, "> $file" || die; | |
print W; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment