当前位置: 技术问答>linux和unix
怎么用shell判断远程ftp是否连接成功
来源: 互联网 发布时间:2016-02-21
本文导语: 我想写一个shell来ftp文件到远程主机上去,但是如果ftp不通的话,文件就传不上传,我想实现如果ftp不通,把文件拷贝到另外的目录下,然后退出shell,这个怎么实现呢? | [code=BatchFile] ...
我想写一个shell来ftp文件到远程主机上去,但是如果ftp不通的话,文件就传不上传,我想实现如果ftp不通,把文件拷贝到另外的目录下,然后退出shell,这个怎么实现呢?
|
[code=BatchFile]
38 #excute upload & record
39 echo "
40 open ${REMOTE_IP}
41 prompt off
42 user ${USER} ${PSWD}
43 lcd ${LOCAL_DIR}
44 cd ${REMOTE_DIR}
45 ${BATCH_PUT}
46 close
47 bye
48 " |ftp -v -n |tee ${TRANSFER_LOG} |sed 's/^/>/g' >> ${RUN_LOG}
49 TOTAL_TRANSFERED=`grep "226 Transfer complete" ${TRANSFER_LOG} |wc -l`
50 echo "TOTAL TRANSFERED: "${TOTAL_TRANSFERED} >> ${RUN_LOG}
51 echo "" >> ${RUN_LOG}
52
53 BATCH_CLEAN=`grep -B 3 "226 Transfer complete." ${TRANSFER_LOG} |grep "local:" |awk -F" " '{printf("'${LOCAL_DIR}'/%s
n",$2);}'`
54
55 #clean uploaded files & record
56 echo "Remove all of transfered files:" >> ${RUN_LOG}
57 COUNT=0
58 for i in ${BATCH_CLEAN}
59 do
60 rm -f ${i}
61 echo ${i}" DELETED." >> ${RUN_LOG}
62 COUNT=`expr $COUNT + 1`
63 done
64 echo "TOTAL DELETED: "${COUNT} >> ${RUN_LOG}
65 echo "" >> ${RUN_LOG}[/code]
38 #excute upload & record
39 echo "
40 open ${REMOTE_IP}
41 prompt off
42 user ${USER} ${PSWD}
43 lcd ${LOCAL_DIR}
44 cd ${REMOTE_DIR}
45 ${BATCH_PUT}
46 close
47 bye
48 " |ftp -v -n |tee ${TRANSFER_LOG} |sed 's/^/>/g' >> ${RUN_LOG}
49 TOTAL_TRANSFERED=`grep "226 Transfer complete" ${TRANSFER_LOG} |wc -l`
50 echo "TOTAL TRANSFERED: "${TOTAL_TRANSFERED} >> ${RUN_LOG}
51 echo "" >> ${RUN_LOG}
52
53 BATCH_CLEAN=`grep -B 3 "226 Transfer complete." ${TRANSFER_LOG} |grep "local:" |awk -F" " '{printf("'${LOCAL_DIR}'/%s
n",$2);}'`
54
55 #clean uploaded files & record
56 echo "Remove all of transfered files:" >> ${RUN_LOG}
57 COUNT=0
58 for i in ${BATCH_CLEAN}
59 do
60 rm -f ${i}
61 echo ${i}" DELETED." >> ${RUN_LOG}
62 COUNT=`expr $COUNT + 1`
63 done
64 echo "TOTAL DELETED: "${COUNT} >> ${RUN_LOG}
65 echo "" >> ${RUN_LOG}[/code]