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 62 63 64 65 66 67
|
#!/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 prompt
$SWITCH="switch";
$PASSEPORT="passeport";
$RADIUS_Y="radius";
$RADIUS_N="no_radius";
$TFTPSERVER='x.x.x.x';
$TELNET = new Net::Telnet (Timeout => 20, Errmode=>'return');
print $logfile("\n--------------------------------------- $jour/$mois/$annee $heure:$min:$sec ---------------------------------------\n");
while ($line = <$file>)
{
($HOST, $NAME, $CODE, $RADIUS) = split(/;/, $line);
if ($TELNET->open($HOST))
{
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("user");
sleep 1;
$TELNET->print("password");
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.ascii address $TFTPSERVER") or die "=> Echec de l'envoi copy running-config \n=> ".$TELNET->errmsg."";
sleep 15;
$TELNET->waitfor("$PROMPT");
$TELNET->print("copy config tftp filename $NAME.bin address $TFTPSERVER") or die "=> Echec de l'envoi copy 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");
}
}
close ($file);
close ($logfile);
<>; #Garde la fenêtre ouverte après la fin de l'exécution du script |
Partager