ssh自动下载SFTP文件

话不多说,开门见山,直接放代码

cron_down_sftp_fill.sh

#!/bin/bash

ip="140.140.140.140"
# 从远处服务器下载文件
PASS="the_real_pwd"
basePath="/home/dev/cron_dev/bill/"

function down_file {
    echo "yest1:$yest1, yest2:$yest2, fileName:$fileName"

    /usr/bin/expect <<EOD
spawn sftp username@$ip:/THE_SFTP_PATH/$yest1/$fileName $filePath
set timeout 300
#第一次ssh连接会提示yes/no,继续
expect {
	"*yes/no" { send "yes\r" }
	"*password" { send "$PASS\r" }
}
expect eof
EOD
}
echo -e "$(date "+%Y-%m-%d %H:%M:%S") down file start"

# 下载近11天的文件
for((i=11;i>=1;i-=1))
do
    month=`date -d "$i day ago" +%Y%m`
    yest1=`date -d "$i day ago" +%Y%m%d`
    yest2=`date -d "$i day ago" +%y%m%d`
    fileName="MERCHANT_${yest2}01_2020"
    filePath="${basePath}${month}/"
    if [ ! -d $filePath ]; then
        mkdir -p $filePath
        # sudo mkdir -p -m 755 $filePath
        echo "sudo mkdir -p ${filePath} done"
    fi
    if [ ! -f "$filePath$fileName" ]; then
        down_file
    else
        echo "$yest1 file exists"
    fi

done

echo -e "$(date "+%Y-%m-%d %H:%M:%S") down file end\n"

posted on 2020-05-26 15:04  aworkstory  阅读(598)  评论(0编辑  收藏  举报

导航