我想编写一个通用的期望脚本来通过 SSH 登录到系统并执行一些命令。我发现的一个例子如下:
#!/usr/bin/expect
set fid [open ./.secret]
set password [read $fid]
close $fid
spawn /usr/bin/ssh root@[lindex $argv 0]
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
#look for the password prompt
}
"*?assword:*" {
send $password
send "\n"
}
}
send -- "PS1='>'\r"
expect -re ">$" { send "hostname\r" }
expect -re ">$" { send "pwd\r" }
...脚本似乎正确登录,但没有执行最后 2 次发送。想法?
编辑:启用 exp_internal 后,我注意到以下内容:
expect: does "" (spawn_id exp4) match glob pattern "*"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) ""
send: sending "PS1='>'\r" to { exp4 }
Gate keeper glob pattern for '>$' is '>'. Activating booster.
expect: does "" (spawn_id exp4) match regular expression ">$"? Gate ">"? gate=no
expect: does "\r\n" (spawn_id exp4) match regular expression ">$"? Gate ">"? gate=no
Last login: Tue Nov 6 14:13:31 2012 from 1.x.x.x
expect: does "\r\nLast login: Tue Nov 6 14:13:31 2012 from 1.x.x.x\r\r\n" (spawn_id exp4) match regular expression ">$"? Gate ">"? gate=no
我正在尝试发送PS1='>'\r
,因为我想覆盖提示。我认为我无法预测提示将是什么,因此,我不知道会出现什么模式。从上面看,提示似乎没有改变。你如何解决这样的问题?