sftp使用

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使用

....
# 限速
#set net:limit-rate 100000:100000
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
#echo "inf:$inf"

3. lftp mirror使用

  1. -R参数,将文件从本地目录传到指定服务器的目录
  2. 无-R参数,可以将指定服务器的目录下文件下载到当前服务器指定目录
....

#-i 包含指定内容
#-v 打印详细信息
#-n 下载新文件
#-r 排除子目录
#-R 本地路径,远程路径
# reg 正则匹配,若有前缀后缀 prefix.*xxx.*suffix

## -n 参数当文件内容修改时候,不会重新传输
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]# stat 1.txt
  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: -

猜你喜欢

转载自blog.csdn.net/u010895512/article/details/123849340
今日推荐