Nachdem das folgende Skript die Inventardatei liest und die Befehle aus der Testbefehlsdatei ausführt, sucht die Foreach-Funktion nach weiteren Informationen aus der Inventardatei zur Verarbeitung und Fehlern anstatt zu enden.Verwenden von Expect Wie beende ich foreach-Funktion nach EOF
#!/usr/bin/expect
set timeout 5
# Open and read hosts from file
set fp [open "inventory_2ps"]
set hosts [split [read $fp]"\n"]
close $fp
# Get commands to run in server
set fh [open "testcommands"]
set commands [split [read $fh] "\n"]
close $fh
# Set login variable
set user "xxxxxxx";
set PW "xxxxxxx";
# Spawn server login
foreach host $hosts {
spawn ssh [email protected]$host
expect "$ "
send "su - xxxxxx\n"
expect "Password: "
send "$PW\n"
expect "$ "
send "xxxxxx -nobash\r"
expect "> "
foreach cmd $commands {
send "$cmd\n"
expect "> "
}
expect eof
erhalten Fehler nach dem letzten Host-Login/Ausfahrt:
>$ spawn ssh [email protected]"
ssh: Could not resolve hostname ": Name or service not known
send: spawn id exp10 not open
while executing
"send "su - xxxxxx\n""
("foreach" body line 6)
invoked from within
"foreach host $hosts {
spawn ssh [email protected]$host
expect "$ "
Ich weiß nicht, warum das eine Perlfrage ist. – mob