我有一个期望脚本,它循环遍历一个包含主机列表的文件,ssh进入每个主机,sudo是root,并运行id。问题是它永远不会返回到源服务器shell。它试图从它已经进入ssh的主机运行,从而弄乱了逻辑。
set file [open "hosts.test"]
set hosts [split [read -nonewline $file] "\n"]
close $file
foreach host $hosts {
puts $host
spawn ssh -q -o StrictHostKeyChecking=no [lindex $argv 0]@$host
expect "Password: "
send "[lindex $argv 1]\r"
expect -re "(>|#) "
send "sudo su -\r"
expect "Enter YOUR password: "
send "[lindex $argv 1]\r"
expect -re "(>|#) "
send "id\r"
expect -re "(>|#) "
send "exit\r"
expect -re "(>|#) "
send "logout\r"
close $spawn_id
}
这是错误:
send: sending "logout\r" to { exp4 }
server.blah.com
spawn ssh -q -o StrictHostKeyChecking=no user@server
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {21856}
expect: does "" (spawn_id exp5) match glob pattern "Password: "? no
Password:
expect: does "Password: " (spawn_id exp5) match glob pattern "Password: "? yes
expect: set expect_out(0,string) "Password: "
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "Password: "
send: sending "passwordhere\r" to { exp5 }
Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster.
expect: does "" (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: does "\r\n" (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
Password:
expect: does "\r\nPassword: " (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: timed out
send: sending "sudo su -\r" to { exp5 }
expect: does "\r\nPassword: " (spawn_id exp5) match glob pattern "Enter YOUR password: "? no
expect: does "\r\nPassword: \r\n" (spawn_id exp5) match glob pattern "Enter YOUR password: "? no
Password:
expect: does "\r\nPassword: \r\nPassword: " (spawn_id exp5) match glob pattern "Enter YOUR password: "? no
expect: timed out
send: sending "passwordhere\r" to { exp5 }
Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster.
expect: does "\r\nPassword: \r\nPassword: " (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: does "\r\nPassword: \r\nPassword: \r\n" (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: read eof
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "\r\nPassword: \r\nPassword: \r\n"
send: sending "id\r" to { exp5 send: spawn id exp5 not open
while executing
"send "id\r""
("foreach" body line 11)
invoked from within
"foreach host $hosts {
puts $host
spawn ssh -q -o StrictHostKeyChecking=no [lindex $argv 0]@$host
expect "Password: "
send "[lindex $argv 1]\r"
e..."
(file "./bootstrap.exp" line 19)