我的代码如下所示。
#!/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/