简体   繁体   English

Bash 脚本在 SFTP 下载期间终止

[英]Bash script is terminated during SFTP download

I have a problem with this script:我对这个脚本有问题:

#!/bin/bash

data="$(date +\%Y\%m\%d)"

PASSW="pippo"

comando0="cd /remote/path"
comando1="get -r bak*"

cd /local/path
mkdir $data

cd /local/path/$data

(/usr/bin/expect -c "
spawn /usr/bin/sftp user@ip

expect \"Password:\"
send \"$PASSW\r\"

expect \"sftp>\"
send \"$comando0\r\"

expect \"sftp>\"
send \"$comando1\r\"

expect \"sftp>\" 
send "bye\r"

expect \"sftp>\"
send \"exit\r\"
")

On the reomte server there are hundreds of directories called bak*.在远程服务器上有数百个名为 bak* 的目录。 I need to download these directories and their contents on my local PC.我需要在本地 PC 上下载这些目录及其内容。 If I connect manually on the remote server and do "get -r bak*" , it works fine.如果我在远程服务器上手动连接并执行 "get -r bak*" ,它工作正常。 After almost two hours I have all the directories on my PC, while if I use the bash script, this is termintaed after 1 or 2 minutes.将近两个小时后,我的 PC 上拥有了所有目录,而如果我使用 bash 脚本,则会在 1 或 2 分钟后终止。

Could you help me ?你可以帮帮我吗 ?

Thanks谢谢

Expect has a default 10-second timeout in the expect request, so if get -r bak* takes hours it will exceed this timeout.期望在expect请求中具有默认的 10 秒超时,因此如果get -r bak*需要几个小时,它将超过此超时。

There's no need to use Expect for this.没有必要为此使用 Expect。 Just feed all the commands to sftp using a here-document.只需使用此处的文档将所有命令提供给sftp

/usr/bin/sftp user@ip <<EOF
$PASSW
$comando0
$comando1
bye
exit
EOF

I've modified the script in this way:我以这种方式修改了脚本:

#!/bin/bash

data="$(date +\%Y\%m\%d)"
PASSW="pippo"
comando0="cd /remote/path"
comando1="get -r bak*"

cd /local/path
mkdir $data
cd /local/path/$data

/usr/bin/sftp user@ip <<EOF
$PASSW
$comando0
$comando1
bye
exit
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM