Bash/Script expect pour SSH
Je suis de nouveau à attendre et de script en général. Je suis en train de faire quelques scripts pour faire de ma vie un peu plus facile quand on tire configurations de périphériques réseau. J'ai réussi à créer une base de script expect SSH à un appareil et d'enregistrer la configuration.
Je veux développer cette et le script pour se connecter à un certain nombre d'adresses IP au lieu d'un seul comme j'ai le droit maintenant. J'ai un fichier nommé list.txt
avec un peu d'adresse IP différente à chaque IP sur une ligne distincte.
De quoi aurais-je besoin de faire pour avoir le script expect se connecter à chacune de ces adresses IP et effectuer le reste des tâches dans le script?
Voici le script expect j'ai jusqu'à présent:
#!/usr/bin/expect -f
#Tells interpreter where the expect program is located. This may need adjusting according to
#your specific environment. Type ' which expect ' (without quotes) at a command prompt
#to find where it is located on your system and adjust the following line accordingly.
#
#
#Use the built in telnet program to connect to an IP and port number
spawn ssh 192.168.1.4 -l admin
#
#The first thing we should see is a User Name prompt
#expect "login as:"
#
#Send a valid username to the device
#send "admin"
#
#The next thing we should see is a Password prompt
expect "Password:"
#
#Send a vaild password to the device
send "password\n"
#
#If the device automatically assigns us to a priviledged level after successful logon,
#then we should be at an enable prompt
expect "Last login:"
#
#Tell the device to turn off paging
#
#After each command issued at the enable prompt, we expect the enable prompt again to tell us the
#command has executed and is ready for another command
expect "admin@"
#
#Turn off the paging
send "set cli pager off\n"
#
#Show us the running configuration on the screen
send "show config running\n"
#
# Set the date.
set date [timestamp -format %C%y%m%d]
#
#Test output sent to file with a timestamp on end
#-noappend will create a new file if one already exists
log_file -noappend /home/test.cfg$date
#
expect "admin@"
#
#Exit out of the network device
send "exit\n"
#
#The interact command is part of the expect script, which tells the script to hand off control to the user.
#This will allow you to continue to stay in the device for issuing future commands, instead of just closing
#the session after finishing running all the commands.`enter code here`
interact
Ai-je besoin de l'intégrer à un script Bash? Si oui, est-il possible de lire une ligne de la list.txt
fichier, utilisez-les comme IP/host variable, puis de lire le suivant et répétez?
- Pouvez-vous configurer vos périphériques à utiliser les clés ssh? Si oui, vous n'avez pas besoin d'attendre du tout.
Vous devez vous connecter pour publier un commentaire.
Je voudrais faire cela (non testé):
Notes:
\r
pour simuler appuyer sur la touche enter lorsque voussend
commandes.expect eof
après l'envoi de "sortie"C'est une version en Perl pour cette question:
Instructions pour l'installation: cpan Attendre
Ce script fonctionne perfectely pour mes besoins.
Param 1: raccordement de la chaîne (ex: [email protected])
Param 2: mot de passe en clair
Param 3: commande à exécuter
Une possibilité est de passer de l'adresse IP en tant que paramètre dans votre script expect:
et ensuite faire un script shell, en appelant votre script expect à l'intérieur d'une boucle while:
ou de l'Utilisation de propriété intellectuelle [gets stdin] pour adresse ip à partir de la saisie de l'utilisateur.
par exemple-
met "Entrez votre adresse ip\n"
set ip [lire stdin]
l'utiliser dans spawn,On peut faire de même pour plusieurs adresses ip à l'aide de la boucle
frayer ssh $ip -l admin