1. expect + sftp简易使用
...
/usr/bin/expect <<EOF
spawn sftp -i $identity_file -P $port $user@ip
expect {
"*yes/no*" { send "yes\r"; exp_continue; }
"*password" { send "${password}\r" }
}
expect "sftp>"
send "cd ${PATH}\r"
expect {
"*Permission*" { send "bye\r"; expect eof; exit 1; }
"*No such file*" { send "bye\r"; expect eof; exit 1; }
"sftp>" { send "\n" }
}
expect "sftp>"
set timeout -1
send "put ${FILE} \r"
expect {
"*Permission*" { send "bye\r"; expect eof; exit 1; }
"*No such file*" { send "bye\r"; expect eof; exit 1; }
"sftp>" { send "bye\r" }
}
expect eof
EOF
2. lftp sftp使用
....
filename="file1"
cd ${localpath}
inf=$(
lftp -u $username,$password sftp://$ip:$port <<EOF
cd ${remotepath}
lcd ${localpath }
set net:limit-rate 100000:100000
put ${filename}
bye
EOF
)
if [ $? -eq 0 ];then
echo "success"
fi
3. lftp mirror使用
- -R参数,将文件从本地目录传到指定服务器的目录
- 无-R参数,可以将指定服务器的目录下文件下载到当前服务器指定目录
....
user_password=$1
username=`echo "$user_password" | awk '{split($1,arr,",");print arr[1]}'`
password=`echo "$user_password" | awk '{split($1,arr,",");print arr[2]}'`
date="xxxx"
filename="file1"
echo "date ["`date +"%Y-%m-%d %H:%M:%S"`" ]"
index=0
while [ $index -ge 0 ]
do
dateinfo=`date -d "$date + ${index} days" +%Y%m%d`
reg+="${dateinfo}@"
index=$(($index-1))
done
reg=${filename%?}
inf=$(
lftp -u $username,$password sftp://$ip:$port <<EOF
mirror -nv -r -i "$reg" -R localpath remotepath
EOF
)
if [ $? -eq 0 ];then
echo "success"
fi
echo "inf:$inf"
查看文件具体时间
[root@chain test]
File: ‘1.txt’
Size: 999 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 792577 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-04-01 20:15:16.615330594 +0800
Modify: 2022-03-24 18:44:13.658618694 +0800
Change: 2022-03-24 18:44:13.658618694 +0800
Birth: -