shell 脚本sftp 文件下载

#!/bin/bash
sftp_Host="192.168.1.1"
sftp_userName="admin"
sftp_passWord="admin"
sftp_port=22
sftpRemotePath="/data/fiels"
sftpLocalPath="/root/sftp"
current=$(date "+%Y-%m-%d %H:%M:%S")

echo "当前时间是:$current"

if [[ $# == 0 ]]; then
    yesterday=$(date "+%Y%m%d" -d "-1 days")
fi
if [[ $# == 1 ]]; then
    yesterday=$1
fi
myDir=$sftpLocalPath
if [[ ! -d $myDir ]]; then
    mkdir -p $myDir
fi
sftpLoadPath=$sftpRemotePath$yesterday
fileFilter=$yesterday*.gz

# SFTP非交互式操作
sftp_download()
{
    expect <<- EOF
    set timeout 5
    spawn sftp  -P $sftp_port $sftp_userName@$sftp_Host

    expect { 
        "(yes/no)?" {send "yes\r"; expect_continue }
        "*assword:" {send "$sftp_passWord\r"}
    }
    expect "sftp>"
    send "cd $sftpLoadPath \r"
    expect "sftp>"
    send "lcd $myDir \r"
    expect "sftp>"
    set timeout -1
    send "mget $fileFilter \r"
    expect "sftp>"
    send "bye\r"
EOF
}

unGzipFiles(){
    cd $myDir
    fileList=`ls *`
    fileArr=($fileList)
    for fileName in ${fileArr[@]}
    do
        echo "开始解压文件:$fileName"
        gzip -d  $fileName
    done

}

reNameFiles(){
    cd $myDir
    fileList=`ls *`
    fileArr=($fileList)

    for fileName in ${fileArr[@]}
    do
        echo "reNameFile :$fileName"
        mv $fileName $fileName".csv"
    done  
}

echo "执行sftp下载操作 : 数据日期:$yesterday"
sftp_download
echo "$yesterday 文件下载完成"
echo "执行解压操作"
unGzipFiles
echo "重命名文件"
reNameFiles

你可能感兴趣的:(shell 脚本sftp 文件下载)