我正在尝试使用send和expect ssh模块在远程UNIX主机上执行命令,但即使脚本成功登录到服务器,它也不会执行命令。
#!/usr/bin/expect
set timeout 60
spawn ssh xxxx@xxxxxx
expect "yes/no" {
send "yes\r"
expect "*?assword" { send "xxxxxx\r" }
} "*?assword" { send "xxxxxxx\r" }
expect "$ "
#sleep 5
send "ps -aef \r"
[xxxxx@xxxxxx Scripts]$ ./TestExpect.sh
spawn ssh xxxxx@xxxxxx
xxxxxx@xxxxxx's password:
Last login: Wed May 9 02:05:47 2018 from xxxxxxxxx
Kickstarted on 2015-05-12
[xxxxx@xxxxx ~]$ [xxxxxx@xxxxx Scripts]$
提示符如下所示
[aacdd123@linprod345 ~]$
答案 0 :(得分:2)
问题可能是因为,您在发送ps -aef
后并不期待任何事情。因此,在打印输出之前,期望的spawn进程已经退出。
在发送ps -aef
send "ps -aef\r"
expect $prompt
send "echo hello\r"
expect $prompt
尝试查看expect_out缓冲区,这将为您提供捕获的流。
puts $expect_out(buffer)