我写了一个脚本,将文件夹从VPS传输到Synology NAS Server。该脚本在1或2%传输时失败,没有任何指示原因。当我手动运行该命令时,它可以正常工作,没有任何问题,它可以完美地传输文件夹。我不确定是什么导致这种情况,如果我能得到一些关于在哪里看的指导,我将不胜感激。
脚本:
#!/usr/bin/expect
set dir [timestamp -format "%Y-%m-%d"]
spawn sftp [email protected]
expect "Password:"
send "passw0rd\n"
expect "sftp>"
send "cd /directory/\n"
expect "sftp>"
send "put -r /home/backup/$dir\n"
expect "sftp>"
send "bye\n"
结果:
root@host [/scripts]# ./sftp.sh
spawn sftp [email protected]
[email protected]'s password:
Connected to host.synology.me
sftp> cd /directory/for/backup
sftp> put -r /home/backup/2017-12-26
Uploading /home/backup/2017-12-26/ to /backup/2017-12-26
Entering /home/backup/2017-12-26/
Entering /home/backup/2017-12-26/accounts
/home/backup/2017-12-26/file1.tar.gz 2% 26MB 1.8MB/s 08:41 ETAroot@host [/scripts]#
root@host [/scripts]#
手动命令:
root@host [/scripts]# sftp [email protected]
[email protected]'s password:
Connected to host.synology.me.
sftp> cd directory
sftp> put -r /home/backup/2017-12-26
Uploading /home/backup/2017-12-26/ to /backups/2017-12-26
Entering /home/backup/2017-12-26/
Entering /home/backup/2017-12-26/accounts
/home/backup/2017-12-26/file.tar.gz 100% 959MB 820.2KB/s 19:57
/home/backup/2017-12-26/file2.tar.gz 100% 147MB 1.4MB/s 01:45
sftp> bye
root@host [/Scripts]#
我想,在你的情况下,Expect
发生超时,因为文件传输比默认超时(10秒)需要一些额外的时间。您可以将timeout
设置为-1,也可以将其增加到更高的值,它们以秒为单位。
set timeout -1; # Infinite amount of waiting time
要么
set timeout 300; # 5 minutes of waiting time
您可以在代码中保留上述语句。
send "put -r /home/backup/$dir\n"
expect {
timeout {puts "timeout happened"}
"sftp>"
}
send "bye\r"
expect eof
始终使用\r
字符发送'Enter'键而不是\n
。最后一个声明expect eof
将等待sftp
计划的优雅关闭。