bash 使用 rsync 和 spawn 通过 SSH 上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20942287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:10:25  来源:igfitidea点击:

Uploading via SSH with rsync and spawn

bashsshexpectrsyncuploading

提问by Jonn

Until very recently I have been using the following bash script to upload any edited files to my server.

直到最近,我一直在使用以下 bash 脚本将任何编辑过的文件上传到我的服务器。

./updatesite.sh

./updatesite.sh

#!/usr/bin/expect -f
spawn rsync -av -e ssh "/(...)/webs" [email protected]:/home/webs
   expect "password:"
   send "xpasswordx\r"
   expect "*\r"
   expect "\r"

Usually it has worked fine. For some reason it has abruptly stopped working several weeks ago. Here is the output it now provides:

通常它运行良好。出于某种原因,它在几周前突然停止工作。这是它现在提供的输出:

[email protected]'s password: 
building file list ... done

As you can see, No files are actually uploaded. But if I paste that exact same command directly into my terminal window without the "spawn," its behavior changes and it uploads the files as usual.

如您所见,实际上没有上传任何文件。但是,如果我将完全相同的命令直接粘贴到我的终端窗口中而没有“生成”,它的行为就会发生变化,并且会像往常一样上传文件。

Here is an example:

下面是一个例子:

Squid:~ John$ rsync -av -e ssh "/(...)/webs" [email protected]:/home/xuserx
[email protected]'s password: 
building file list ... done
webs/somefile.txt

sent 878 bytes  received 42 bytes  204.44 bytes/sec
total size is 96409  speedup is 104.79
Squid:~ John$ 

Do you know what could be causing this?

你知道是什么原因造成的吗?

回答by glenn Hymanman

#!/usr/bin/expect -f
# exp_internal 1    ;# uncomment to turn on expect debugging
set timeout -1
spawn rsync -av -e ssh "/(...)/webs" [email protected]:/home/webs
expect "password:"
send "xpasswordx\r"
expect eof

It may be a timeout issue, so set the timeout to infinite. Since you don't have to interact with rsync in any way except for the password, just wait for it to finish (expect eof).

这可能是超时问题,因此将超时设置为无限。由于除了密码之外,您不必以任何方式与 rsync 交互,只需等待它完成(期待 eof)。