Code :
#!/usr/bin/perl use strict; use warnings; use autodie; use English qw(-no_match_vars ); use Net::Telnet::Cisco; use Net::Telnet; # donnée persistente avec une closure { my $i; } my $backup = {host => '192.***.**.*', dir => 'cisco', dir2 => 'HP', dir3 => 'linksys'}; my $cisco = {host=> undef, log => 'R2T', pass => 'bonjour', enapass=> 'cisco'}; my $HP = {host => undef, log => 'R2T', pass => 'bonjour'}; my $LK = {host => undef, log => 'R2T', pass => 'bonjour'}; ########Ouverture du fichier ##############" open(my $fh, '<', '/var/lib/rancid/all/router.db'); # le fichier où ce trouve un partie des adresses IP, Comment rajouter d'autre fichier??? foreach (<$fh> ) { if (/([0-9.]{1,})\:cisco/) { $cisco->{host}=$1; &BackupCiscoSwitch($cisco, $backup); } elsif (/([0-9.]{1,})\:hp/) { $HP->{host} = $1; &BackupHpSwitch($HP, $backup); } elsif (/([0-9.]{1,})\:linksys/) { $LK->{host}=$1; &BackupLinkSwitch($LK, $backup); } } #backup pour switch cisco sub BackupCiscoSwitch($$) { say $cisco->{host}; my $session = Net::Telnet::Cisco->new(Host => $cisco->{host}, errmode => sub {&error}); if ($session->login ( $cisco-> {log}, $cisco-> {pass }) ) { # Enable mode if ($session->enable($cisco->{enapass}) ) { $session->cmd("copy run tftp://".$backup->{host}.'/'.$backup->{dir}.'/'.$cisco->{host}.'-'.today().".cfg\n\n\n" ); $session->close; } else { } } else { } } #backup pour switch HP procurve sub BackupHpSwitch ($$) { say $HP->{host}; my $session = new Net::Telnet( Timeout =>10, Telnetmode => 0, errmode => sub {&error}, Dump_Log => "logit.txt" ); $session->open($HP->{host}); $session->print('') if $session->waitfor('/Press any key to continue/') == 1; #$session->dump_log("logit.txt" ); $session->print ($HP-> {log}) if ($session->waitfor ('/Username:/') == 1); $session->print($HP->{pass}) if ($session->waitfor('/Password:/') == 1); say "Ok"; $session->dump_log("logit.txt" ); $session->cmd("copy running-config tftp ".$backup->{host}.' '.$backup->{dir2}.'/'.$HP->{host}.'-'.today().".cfg\n\n\n" ) if ($session->waitfor('/#/') == 1); $session->close; } #bachup pour switch Linksys sub BackupLinkSwitch($$) { say $LK->{host}; my $session= new Net::Telnet (Timeout=>5, Telnetmode=>0, errmode => sub {&error}); #$session->dump_log("logit.txt" ); $session->open ($LK-> {host }); sleep (1); $session->print('') if ($session->waitfor('/Execute/') == 1); $session->print ($LK-> {log}); $session->print($LK->{pass}); $session->put("\e" ); $session->put("\t" ); $session->put("\n" ); $session->put("\n" ); $session->put("\cZ" ); $session->print('lcli'); $session->print ($LK-> {log}) if ($session->waitfor ('/User Name:/') == 1); $session->print($LK->{pass}) if ($session->waitfor('/Password:/') == 1); say "Ok"; $session->cmd("copy startup-config tftp://".$backup->{host}.'/'.$backup->{dir3}.'/'.$LK->{host}.'-'.today().".cfg\n\n\n" ) if ($session->waitfor('/#/') == 1); $session->close; } sub error { print "connexion échoué\n"; }
|