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

标签 timeout wait expect

我的代码如下所示。

#!/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 脚本执行)完成。 然后我想退出远程 session 。

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

set somelocation "cd /somelocation"

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

最佳答案

发送某些内容后,您需要期待某些内容。当您发现自己将 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 模式是否正确匹配。

关于timeout - 我怎样才能等到发送命令在expect脚本中完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61920243/

相关文章:

linux - 使用 '&' 和 'wait' 时后台进程不会死亡

ssh - 如何在 TCL Expect 中的多个程序之间共享生成的 SSH 进程?

mysql - docker : accessing MySQL container during Tomcat image build

linux - 从 Expect 捕获返回码

java - 无法使用 memcached 的 Membase 客户端库连接到 AWS ElastiCache 集群

java - 在 Jena 自定义 PropertyFunction 中等待服务器响应

C++ 11 替代 pthread_cond_timedwait

IPHONE SDK : NSURLConnection asynchronous/wait completion bug ?

java - 如何在主线程上围绕 java 中的某些代码设置超时?

.net - 如何增加asp.net中web服务的超时时间?