1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#!/usr/bin/perl
use Net::Telnet;
########### DATE & TIME##############################################################################################
($sec,$min,$heure,$jour,$mois,$annee)= localtime time;
$annee+=1900;
$mois+=1;
###########OPENING FILE#############################################################################################
unless (open ($file, "switch.txt")) { die ("cannot open file Switch.txt\n"); } #fichier qui contient les adresses et noms des switchs
unless (open ($logfile, ">>syslog.txt")) { die ("cannot open file Log.txt\n"); } #fichier qui contient les sessions telnet échouées
#############Variables################################################################################################
$PROMPT='/.*[\$#:>\]\%] *$/'; #prend en compte n'importe caractéres du promp
$TFTPSERVER='10.1.1.1.';
$TELNET = new Net::Telnet (Timeout => 20, Errmode=>'return');
$YES='yes';
print("
#####################################################################
##### TFTP Bacbup is Started ######
##### Please don't close the window ######
#####################################################################\n");
print $logfile("\n--------------------------------------- $jour/$mois/$annee $heure:$min:$sec ---------------------------------------\n");
while ($line = <$file>)
{
($HOST, $NAME, $PASSSWITCH, $RADIUS) = split(/,/, $line);
if ($TELNET->open($HOST) && $PASSSWITCH eq "switch" && $RADIUS eq "yes")
{
sleep 1;
$TELNET->waitfor("Enter Ctrl-Y to begin.");
$TELNET->put("\031") or die "=> cannot send Ctrl+Y \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->print("********");
sleep 1;
$TELNET->print("*******");
sleep 5;
$TELNET->waitfor("IP Configuration/Setup...");
$TELNET->print("c") or die "=> cannot send C \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->waitfor("$PROMPT");
$TELNET->print("enable") or die "=> cannot send enable \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->waitfor("$PROMPT");
$TELNET->print("copy running-config tftp filename $NAME.ac address $TFTPSERVER") or die "=> Echec de l'envoi copy running-config \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->close;
}
else
{
print $logfile ("cannot to make the backup for: $HOST $NAME at $jour/$mois/$annee $heure:$min:$sec\n");
}
}
close ($file);
close ($logfile);
print("
######################################################################
##### TFTP Bacbup is Completed #####
##### Please press any key to quit #####
######################################################################");
<>; #Garde la fenêtre ouverte après la fin de l'exécution du script |