linux – 期待脚本等待命令挂起

前端之家收集整理的这篇文章主要介绍了linux – 期待脚本等待命令挂起前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个嵌入在bash脚本中的expect脚本:
#! /bin/bash

# this function runs a command like 'ssh' and provides the password
function with_password {
expect << END
spawn $2
expect *assword:*
send -- $1
interact
wait
END
}

# run "long_running_command" on the remote server
with_password my_password "ssh my_user@some-server long_running_command"

# rsync some data to the remote server
with_password my_password "rsync /some/dir my_user@some-server:/remote/dir"

# run some other random command
with_password my_password "ssh my_user@some-server some_other_command"

问题是有时脚本会在等待生成命令时挂起.如果我
将wait命令退出expect脚本,命令将继续在远程上运行
服务器,但bash脚本将继续,我无法知道它何时完成.

为什么我的期望脚本似乎随机挂起?

解决方法

expect here-doc中的interact命令就是问题所在.我需要等待EOF:
#! /bin/bash

# this function runs a command like 'ssh' and provides the password
function with_password {
expect << END
set timeout 900
spawn $2
expect *assword:*
send -- $1
expect EOF
END
}

# run "long_running_command" on the remote server
with_password my_password "ssh my_user@some-server long_running_command"

# rsync some data to the remote server
with_password my_password "rsync /some/dir my_user@some-server:/remote/dir"

# run some other random command
with_password my_password "ssh my_user@some-server some_other_command"

当ssh / rsync命令在正确的时间(可能是?)关闭输入时,有时交互命令似乎有效,但它不可靠.

原文链接:https://www.f2er.com/linux/396211.html

猜你在找的Linux相关文章