1.shell脚本后台上传、下载文件到 sftp
vim test.sh
#!/bin/bash
lftp -u 账号,'密码' -p 端口 sftp://xxxx:端口 <<EOF
# 批量上传本地文件
cd /path # sftp路径
mput /path # 本地服务器路径
# 批量下载sftp文件到本地
cd /path 本地服务起目录
lcd /path sftp目录
mget testfile*
sleep 3
bye
EOF
注意:sftp下载单个文件:get,上传单个文件:put
2. exp后台上传、下载文件到sftp
vim test.exp
#!/usr/bin/expect
# Set variables
set user "sftp账号"
set password "sftp密码"
set host "sftp ip"
set port "sftp端口"
# 服务器本地路径
set localFile "/path";
# sftp路径
set remoteDir "/path";
set yesterday [clock format [clock scan "yesterday"] -format "%Y%m%d"]
set today [clock format [clock seconds] -format "%Y%m%d"]
#set two_days_ago_date
#set now [clock seconds]
#set two_days_ago [expr {$now - 2 * 86400}]
#set two_days_ago_date [clock format [clock scan "now - 2 days"] -format "%Y%m%d"]
spawn sftp -oConnectTimeout=300 -oPort=$port $user@$host
expect "password:";send "$password\r"
expect "sftp>";send "cd $remoteDir\r"
expect "sftp>";send "lcd $localFile\r"
# 批量上传本地文件
expect "sftp>";send "mput testfile\r"
# 批量下载本地文件
expect "sftp>";send "mget testfile\r"
set timeout 300
expect "sftp>";send "bye\r"
expect eof