Unix - 在执行命令时期望不工作

时间:2018-05-09 06:15:32

标签: linux shell unix expect send

我正在尝试使用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 ~]$

1 个答案:

答案 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)