通过sftp提取最后一个目录

通过sftp提取最后一个目录

我有一个脚本,允许您通过 sftp 连接到服务器,我想要的是转到一个目录并带来使用所有子文件夹和文件创建的最后一个目录,尽管我在这里作为示例的目录是错误的,什么它只是提取所有文件夹而不放入文件,我做错了什么?非常感谢

#!/bin/bash 
HOST=192.168.1.133 
PORT=22 
USER=root 
PASSWORD=password 
SOURCE_FILE=/root/logs 
DIA=`date +"%d/%m/%Y"` 
HORA=`date +"%H:%M"` 
TIME=`$DAY+$HOUR` 
TARGET_DIR="ls -td -- */ | head -n 1" 
/usr/bin/expect<<EOD > output.log 
spawn /usr/bin/sftp -o Port=$PORT $USER@$HOST 
expect "password:" 
send "$PASSWORD\r" 
expect "sftp>" 
send "get -r $SOURCE_FILE $TIME $TARGET_DIR\r" 
expect "sftp>" 
send "bye\r" 
EOD 
RC=$? 
if [[ ${RC} -ne 0 ]]; then   
   cat output.log | mail
-s "Errors Received" "[email protected]" 
else   
    echo "Success" | mail -s "Transfer Successful" "[email protected]" 
fi

答案1

我已经解决了这个问题,是这样的,以防有人需要,问候。

#!/bin/bash

HOST=192.168.1.123

PORT=3355

USER=lab

PASSWORD=demo

DIR=$(date +"%Y-%m-%d")

 

/usr/bin/expect<<EOD > output.log

spawn /usr/bin/sftp -o Port=$PORT $USER@$HOST

expect "password:"

send "$PASSWORD\r"

expect "sftp>"

send "cd logs\r"

expect "sftp>"

send "mget -r $DIR\r"

expect "sftp>"

send "bye\r"

EOD

RC=$?

 

#if [[ ${RC} -ne 0 ]]; then

#  cat output.log | mail -s "Errors Received" [email protected]

#else

#  echo "Success" | mail -s "Transfer Successful" [email protected]

#fi

相关内容