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
|
#!/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, ">>Log.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
$HOST;
$NAME;
$TFTPSERVER='10.5.1.53';
$TELNET = new Net::Telnet (Timeout => 20, Errmode=>'return');
while ($line = <$file>)
{
($HOST, $NAME) = split(/;/, $line);
if ($TELNET->open($HOST))
{
sleep 1;
$TELNET->waitfor("Enter Ctrl-Y to begin.");
$TELNET->print("\031") or die "=> cannot send Ctrl+Y \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->waitfor("Enter Username: ");
$TELNET->print("Dupont\n") or die "=> cannot send Ctrl+Y \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->waitfor("Enter Password: ");
$TELNET->print("dupont\n") or die "=> cannot send Ctrl+Y \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->waitfor("IP Configuration/Setup...");
$TELNET->print("c") or die "=> cannot send C \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->waitfor("$prompt");
$TELNET->print("en\n") or die "=> cannot send enable \n=> ".$TELNET->errmsg."";
sleep 1;
$TELNET->waitfor("$prompt");
$TELNET->print("copy config tftp filename $NAME address $TFTPSERVER\n") or die "=> Echec de l'envoi copy config \n=> ".$TELNET->errmsg."";
sleep 1;
}
else
{
print $logfile ("cannot open session telnet at $jour/$mois/$annee $heure:$min:$sec for $HOST $NAME");
}
}
print $logfile("\n--------------------------------------- $jour/$mois/$annee $heure:$min:$sec ---------------------------------------\n");
close ($file);
close ($logfile);
#<>; #Garde la fenêtre ouverte après la fin de l'exécution du script |
Partager