我怎样才能等到发送命令在 expect 脚本中完成执行

How can I wait till the send command completes its execution in expect script

我的代码是这样的。

#!/usr/bin/expect -f

set $somelocation "cd /somelocation"
set perlScriptCommand "perl runScript.pl abc xyz\r"

spawn ssh uid@IP
expect "Password: "
send "$pwd\r"
sleep 2

#change directory in the remote
send "$somelocation"

#run a perl script in the remote
send $perlScriptCommand

# **I want to make the script wait till the perl command is completed in remote

#once the previous step is completed I want to exit the remote session
send "exit\r"
expect eof

我想等到发送命令(远程执行 perl 脚本)完成。 然后我想退出远程会话。

我试过将命令与 ssh 一起传递。它仍然不等待 perl 脚本完成。

set somelocation "cd /somelocation"

spawn ssh uid@IP "$somelocation; $perlScriptCommand"

在你 send 之后,你还需要 expect 一些东西。当您发现自己将 sleep 放入期望脚本时,您的期望模式可能不匹配,需要进行调整。

我假设您的提示以 "dollar+space"

结尾
set prompt {$ $}
set somelocation "cd /somelocation"
set perlScriptCommand "perl runScript.pl abc xyz"

spawn ssh uid@IP
expect "Password: "
send -- "$pwd\r"
expect -re $prompt

send -- "$somelocation\r"
expect -re $prompt

# disable the timeout so the perl script can take as long as it needs
set timeout -1
send -- "$perlScriptCommand\r"
expect -re $prompt

send "exit\r"
expect eof

在开发 expect 脚本时,运行 使用 expect -d 以便您可以验证您的 expect 模式是否正确匹配。