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

前端之家收集整理的这篇文章主要介绍了linux – 期待脚本等待命令挂起前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个嵌入在bash脚本中的expect脚本:
  1. #! /bin/bash
  2.  
  3. # this function runs a command like 'ssh' and provides the password
  4. function with_password {
  5. expect << END
  6. spawn $2
  7. expect *assword:*
  8. send -- $1
  9. interact
  10. wait
  11. END
  12. }
  13.  
  14. # run "long_running_command" on the remote server
  15. with_password my_password "ssh my_user@some-server long_running_command"
  16.  
  17. # rsync some data to the remote server
  18. with_password my_password "rsync /some/dir my_user@some-server:/remote/dir"
  19.  
  20. # run some other random command
  21. with_password my_password "ssh my_user@some-server some_other_command"

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

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

@H_502_11@解决方法
expect here-doc中的interact命令就是问题所在.我需要等待EOF:
  1. #! /bin/bash
  2.  
  3. # this function runs a command like 'ssh' and provides the password
  4. function with_password {
  5. expect << END
  6. set timeout 900
  7. spawn $2
  8. expect *assword:*
  9. send -- $1
  10. expect EOF
  11. END
  12. }
  13.  
  14. # run "long_running_command" on the remote server
  15. with_password my_password "ssh my_user@some-server long_running_command"
  16.  
  17. # rsync some data to the remote server
  18. with_password my_password "rsync /some/dir my_user@some-server:/remote/dir"
  19.  
  20. # run some other random command
  21. with_password my_password "ssh my_user@some-server some_other_command"

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

猜你在找的Linux相关文章