Logs

scp 命令記錄錯誤

  • September 21, 2020

我正在從 server1 執行一個腳本,該腳本有責任根據使用 scp 命令的特定參數將文件從 server2(將是參數 ${Server})複製到 server3:

scp -p -qo StrictHostKeyChecking=no user@${Server}:/path/to/source/files/*${EAPdate}* user@hostname3:/path/to/destination/files/

腳本本身應該在執行時創建一個日誌。如果一切都按預期工作並且 EAPdate 參數按應有的方式鍵入 (YYYYMMDD),則複製命令按預期工作。

但是,如果參數鍵入為 (DDMMYYYY),則複製命令將失敗,並出現 No such file or directory 錯誤。

腳本如下:

#!/bin/bash

configuration_file=copy_EAP_files.ini

hostname=$(hostname)
script=$(basename "$0")
start_time=$(date)
start_seconds=${SECONDS}
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
LOGFILE="$(basename $0)_$(date +%Y.%m.%d_%H.%M.%S.log)"
LOGPATH=${DIR}"/logs"
mkdir -p ${LOGPATH}

echo_with_timestamp_and_log() {
   message=$1
   echo "$(date +"%Y.%m.%d %H:%M:%S") ${message}" | tee -a ${LOGPATH}/${LOGFILE}
}

log_time () {
       echo -n "$(date +"%Y.%m.%d %H:%M:%S") "
}

read_parameter() {
       variableToRead=$1
       errorCodeToThow=$2
       # read only lines begining with parameter name
       result="$(grep "^$variableToRead" ./${configuration_file} | cut -d' ' -f2)"
       if [ "${result}" == "" ]; then
               echo_with_timestamp_and_log "ERROR ${2}: $1 variable not found in *.ini file"
               exit $2
       else
               echo "${result}"
       fi
}

echo_with_timestamp_and_log "Host: ${hostname}"
echo_with_timestamp_and_log "Script: ${script}"
echo_with_timestamp_and_log "Start time: ${start_time}"
echo_with_timestamp_and_log "______________ Script start _______________"

if [ "$#" -eq 0 ]; then
   Server=$(read_parameter Server 101)
   EAPdate=$(read_parameter EAPdate 102)
elif [ "$#" -eq 2 ]; then
   Server=$1
   EAPdate=$2
else
   echo_with_timestamp_and_log "Illegal number of parameters"
   exit 107
fi

echo_with_timestamp_and_log "server = ${Server}"
echo_with_timestamp_and_log "date = ${EAPdate}"
echo_with_timestamp_and_log "Connecting to AIS server ${Server}"
ssh -T $Server <<EOF | tee -a ${LOGPATH}/${LOGFILE}
 $(typeset -f log_time)
 log_time
 echo "${Server}: Copying the EAP files to designated FTP path"
 echo ""
 echo "${Server}: Copying the files..."
 scp -p -qo StrictHostKeyChecking=no user@${Server}:/path/to/source/files/*${EAPdate}* user@hostname3:/path/to/destination/files/
EOF

echo_with_timestamp_and_log "______________ Script end _________________"
elapsed_time=$((${SECONDS} - ${start_seconds}))
readable_time="$((${elapsed_time}/3600)) h $((${elapsed_time}%3600/60)) min"
echo_with_timestamp_and_log "Host: ${hostname}"
echo_with_timestamp_and_log "Script: ${script}"
echo_with_timestamp_and_log "End time: $(date)"
echo_with_timestamp_and_log "Duration: ${readable_time}"

*現在,如果 scp 腳本會導致${EAPdate}*參數出現錯誤“沒有此類文件或目錄” ,則日誌將不會擷取此消息。而且我不明白為什麼-結果日誌沒有擷取腳本執行時在螢幕上看到的所有消息。

我錯過了什麼?我應該如何創建 scp 命令以根據執行在日誌中傳遞以下輸出:

  • 成功: echo “文件複製成功!”
  • 由於“沒有這樣的文件或目錄”錯誤而失敗:回顯“文件未成功複製!源路徑中沒有這樣的文件或目錄!”

根據man bash,§pipeline

如果使用 |&,命令的標準錯誤,除了它的標準輸出,還連接到…

你需要更換

ssh -T $Server <<EOF | tee -a ${LOGPATH}/${LOGFILE}

經過

ssh -T $Server <<EOF |& tee -a ${LOGPATH}/${LOGFILE}

引用自:https://unix.stackexchange.com/questions/610490