Unix - Expect 在执行命令时不工作
Unix - Expect Not Working While Executing Commands
我正在尝试使用 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 ~]$
问题可能是因为您在发送 ps -aef
后没有期待任何事情。因此,在打印输出之前,expect spawn 进程已经退出。
尝试在发送后添加更多命令ps -aef
send "ps -aef\r"
expect $prompt
send "echo hello\r"
expect $prompt
也尝试查看 expect_out 缓冲区,这将为您提供捕获的流。
puts $expect_out(buffer)
我正在尝试使用 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 ~]$
问题可能是因为您在发送 ps -aef
后没有期待任何事情。因此,在打印输出之前,expect spawn 进程已经退出。
尝试在发送后添加更多命令ps -aef
send "ps -aef\r"
expect $prompt
send "echo hello\r"
expect $prompt
也尝试查看 expect_out 缓冲区,这将为您提供捕获的流。
puts $expect_out(buffer)