概述

 因公司项目由之前的ftp换位sftp,且之前的UDP函数未开发出来,使用脚本实现部分功能(sftp文件下载)。一次连接和多次连接均为指定文件名下载,下载最新的文件即下载修改时间最新的文件,因每天最多更新一份,在下载最新文件脚本中以天为单位。脚本内容如下。

一次连接

#!/bin/bash
#  -------------+---------------------------------
# * Filename    :       sftp_download_nowfile.sh
# * Date        :       2020-08-24 
# * Author      :       wangjianxiong           
# * Description :                       
#  -------------+---------------------------------

MYPATH=$(dirname $0)
MYPWD=`cd $MYPATH/../ && pwd`

IP=59.255.188.30
PORT=5522
USER=ftpuser
PASSWORD=!QAZ2wsx12345

SFTPDIR=/data/file/xy
FILE_NAME=20200818.zip

find $MYPWD/data -type f -name "*" -exec rm -rf {} \;

[ -f sftp_get.sh ] && rm -rf sftp_get.sh

echo "lftp -u '${USER}','${PASSWORD}' sftp://${IP}:${PORT} <<EOF"  >> sftp_get.sh

cat $MYPATH/../conf/list_dir | awk -F"." '{print $NF}' > new_list
for i in `cat new_list`
do
    lpath=`cd $MYPWD/data$i; pwd`
    rpath=$SFTPDIR$i

    echo "cd $rpath" >> sftp_get.sh
    echo "lcd $lpath" >> sftp_get.sh
    echo "get $FILE_NAME" >> sftp_get.sh
done

echo "bye" >> sftp_get.sh
echo "EOF" >> sftp_get.sh

chmod a+x sftp_get.sh
View Code

多次连接

for循环实现

#!/bin/bash
#  -------------+---------------------------------
# * Filename    :       sftp_download_nowfile.sh
# * Date        :       2020-08-24 
# * Author      :       wangjianxiong           
# * Description :                       
#  -------------+---------------------------------

MYPATH=$(dirname $0)
MYPWD=`cd $MYPATH/../ && pwd`

IP=59.255.188.30
PORT=5522
USER=ftpuser
PASSWORD=!QAZ2wsx12345

SFTPDIR=/data/file/xy
FILE_NAME=20200818.zip

find $MYPWD/data -type f -name "*" -exec rm -rf {} \;

cat $MYPATH/../conf/list_dir | awk -F"." '{print $NF}' > new_list
for i in `cat new_list`
do
    sleep 1

    lpath=`cd $MYPWD/data$i; pwd`
    rpath=$SFTPDIR$i

    lftp -u '${USER}','${PASSWORD}' sftp://${IP}:${PORT} <<EOF
    cd $rpath
    lcd $lpath
    get $FILE_NAME
    bye
EOF
done
View Code

 下载最新文件

for 嵌套 while

#!/bin/bash
#  -------------+---------------------------------
# * Filename    :       lftp_get.sh
# * Date        :       2020-08-28
# * Author      :       wangjianxiong
# * Description :
#  -------------+---------------------------------

MYPATH=$(dirname $0)
MYPWD=`cd $MYPATH/../ && pwd`

SFTPDIR=/data/file/xy

find $MYPWD/data -type f -name "*" -exec rm -rf {} \;

cat $MYPATH/../conf/list_dir | awk -F"." '{print $NF}' > new_list
for i in `cat new_list`
do
    lpath=`cd $MYPWD/data$i; pwd`
    rpath=$SFTPDIR$i

    i=0
    while true
    do
        lftp -u 'ftpuser','!QAZ2wsx12345' sftp://59.255.188.30:5522 <<EOF
        mirror  --newer-than='now-${i}day' --only-newer --verbose $rpath $lpath
        bye
EOF

    ret=`ls $lpath`
    if [ "`ls -A $lpath`" = "" ]; then
        i=$(($i+1))
    else
        break
    fi
    done
done
View Code