在脚本命令行SFTP会话中检测上载成功/失败?

我正在编写一个BASH shell脚本,将目录中的所有file upload到远程服务器,然后删除它们。 它将通过CRON工作每隔几个小时运行一次。

我的完整脚本如下。 基本的问题是应该确定文件是否成功上传的部分不起作用。 无论上传是否成功,SFTP命令的退出状态始终为“0”。

我怎样才能知道一个文件是否正确上传,以便我可以知道是删除它还是让它成为?

#!/bin/bash # First, save the folder path containing the files. FILES=/home/bob/theses/* # Initialize a blank variable to hold messages. MESSAGES="" ERRORS="" # These are for notifications of file totals. COUNT=0 ERRORCOUNT=0 # Loop through the files. for f in $FILES do # Get the base filename BASE=`basename $f` # Build the SFTP command. Note space in folder name. CMD='cd "Destination Folder"\n' CMD="${CMD}put ${f}\nquit\n" # Execute it. echo -e $CMD | sftp -oIdentityFile /home/bob/.ssh/id_rsa [email protected] # On success, make a note, then delete the local copy of the file. if [ $? == "0" ]; then MESSAGES="${MESSAGES}\tNew file: ${BASE}\n" (( COUNT=$COUNT+1 )) # Next line commented out for ease of testing #rm $f fi # On failure, add an error message. if [ $? != "0" ]; then ERRORS="${ERRORS}\tFailed to upload file ${BASE}\n" (( ERRORCOUNT=$ERRORCOUNT+1 )) fi done SUBJECT="New Theses" BODY="There were ${COUNT} files and ${ERRORCOUNT} errors in the latest batch.\n\n" if [ "$MESSAGES" != "" ]; then BODY="${BODY}New files:\n\n${MESSAGES}\n\n" fi if [ "$ERRORS" != "" ]; then BODY="${BODY}Problem files:\n\n${ERRORS}" fi # Send a notification. echo -e $BODY | mail -s $SUBJECT [email protected] 

由于一些使我头部受伤的操作考虑因素,我不能使用SCP。 远程服务器在Windows上使用WinSSHD,并且没有EXEC权限,因此任何SCP命令都会失败,并显示消息“执行请求在通道0上失败”。 因此上传必须通过交互式SFTP命令完成。

如果您批量您的SFTP会话,退出状态$? 会告诉你,如果这是一个失败的转移或不。

 echo "put testfile1.txt" > batchfile.txt sftp -b batchfile.txt user@host if [[ $? != 0 ]]; then echo "Transfer failed!" exit 1 else echo "Transfer complete." fi 

编辑添加:这是我们在工作中使用的生产脚本,所以我相信它是有效的。 我们不能使用scp,因为我们的一些客户使用SCO Unix。

服务器故障的传统不是过分怀疑前提条件,但我必须问:是不是可以通过SMB安装远程文件系统,或者2)使用fuse / sshfs代替? (我假设发送的机器是一个Linux机器,因为你使用的是bash和ssh。)

要真正回答你的问题,我认为你的问题很简单。 考虑:

 quest@wonky:~$ false quest@wonky:~$ if [ $? != "0" ] ; then echo asdf ; fi asdf quest@wonky:~$ if [ $? != "0" ] ; then echo asdf ; fi 

试试像这样:

 quest@wonky:~$ false quest@wonky:~$ res=$? quest@wonky:~$ if [ $res != "0" ] ; then echo asdf ; fi asdf quest@wonky:~$ if [ $res != "0" ] ; then echo asdf ; fi asdf 

解释:你的第二条语句“if [$?!=”0“];然后”testing最新语句的退出状态,不再是sftp,而是前面的if语句。

然后我想知道,如果它有上传文件的问题,真的会退出非零? 粗略testing表明我的不是。

使用rsync(.exe)并挖掘--remove-source-files &co。,甚至不关心失败的上传,他会! 😉

是的,你可以把它和ssh绑在一起,看看 – --rsh

锅盖头

如何在Windows操作系统上运行sftp批处理脚本来检测成功/失败?

我使用%errorlevel%来检测成功/失败,有时候%errorlevel%被返回0,但是这个会话失败,文件没有上传到服务器。 我的脚本在下面

我如何检测sftp正确上传? THX这么多

 @echo off (echo cd %4 echo put %2 echo exit)>sftpScript.txt sftp -b sftpScript.txt %3 set result=%errorlevel% if result EQU 0 ( del sftpScript.txt del %2 ) else ( del sftpScript.txt ) exit %result% 

**编辑以实际使用batch file展开基于批处理的解决scheme。
在我正在运行的sftp的副本上,即使有多个命令超出了错误点,batch file也会退出

通过最初发布的逻辑,通过检查$?,您正在testing整个批处理的结果。 如果您可以删除某些已成功传输的文件,则可以在每次放置后使用!rm生成batch file。 如果投入失败,公司将不会运行

 (for F in *.pgp do echo put $F echo !rm $F done;echo quit) > Batchfile ; sftp -b Batchfile user@targetServer