加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 02:41:48 所属栏目:Linux 来源:网络整理
导读:我有一个嵌入在bash脚本中的expect脚本: #! /bin/bash# this function runs a command like 'ssh' and provides the passwordfunction with_password {expect ENDspawn $2expect *assword:*send -- $1interactwaitEND}# run "long_running_command" on the
我有一个嵌入在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命令在正确的时间(可能是?)关闭输入时,有时交互命令似乎有效,但它不可靠.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读