Bash/Expect-Skript für den SSH -

Ich bin neue zu erwarten und scripting im Allgemeinen. Ich bin versucht zu machen, ein paar Skripte, um mein Leben ein wenig leichter beim ziehen Konfigurationen von Netzwerkgeräten. Ich schaffte es zum erstellen einer Basis-rechnen-Skript um per SSH auf ein Gerät und speichern Sie die Konfiguration.

Möchte ich erweitern auf dies und Erlaube dem Skript, um eine Verbindung zu einer Anzahl von IP-Adressen statt nur einem, wie ich jetzt haben. Ich habe eine Datei namens list.txt mit ein paar verschiedene IP-Adressen jede IP-Adresse auf einer separaten Zeile.

Was müsste ich tun, um das erwarten-Skript Verbindung zu jeder dieser IP-Adressen und übernehmen Sie den rest der Aufgaben im Skript?

Hier ist das Erwarten Skript habe ich bisher:

#!/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

Muss ich integrieren, das mit einem Bash-Skript? Wenn dem so ist, ist es möglich, zum Lesen einer Zeile von der list.txt - Datei verwenden, die die IP/host-variable und Lesen Sie dann die nächste und wiederholen?

Können Sie die Konfiguration Ihrer Geräte für die Verwendung von ssh-keys? Wenn ja, brauchen Sie nicht erwarten, auf alle.

InformationsquelleAutor minorix | 2013-09-30

Schreibe einen Kommentar