使用 ssh 和 scp 时出现“丢失连接”错误

使用 ssh 和 scp 时出现“丢失连接”错误
mySCP="scp -q -o StrictHostKeyChecking=no -o BatchMode=yes -o UserKnownHostsFile=/dev/null -o HostbasedAuthentication=no"

rm -Rf /tmp/logdata
mkdir /tmp/logdata

$mySCP -r xyz2384:/d/d1/run_logs/results/trail/log.txt /tmp/logdata/
myResult=$?

if (( $myResult == 0 )); then
    echo "SUCCESS – Log data copying, at $(date +'%m/%d/%Y %H:%M:%S')."
else
    echo "**FAILED – Log data copying, at $(date +'%m/%d/%Y %H:%M:%S')."
    echo "$myResult"
    exit
fi

当我将其作为脚本的一部分执行时,有时会收到“ lost connection”错误并且脚本的执行终止,但有时它可以正常工作。

是否有某种方法可以检查连接可用性并重试直到一切成功执行?

答案1

假设其余代码有效:

mySCP="scp -q -o StrictHostKeyChecking=no -o BatchMode=yes -o UserKnownHostsFile=/dev/null -o HostbasedAuthentication=no"

rm -Rf /tmp/logdata
mkdir /tmp/logdata

myCmd="$mySCP -r xyz2384:/d/d1/run_logs/results/trail/log.txt /tmp/logdata/"

while true; do
    $myCmd
    myResult=$?
    date=$(date +'%m/%d/%Y %H:%M:%S')
    if [ $myResult -eq 0 ]; then
        echo "SUCCESS – Log data copying, at $date."
        break
    else
        echo "**FAILED – Log data copying, at $date."
        echo "mySCP return code: $myResult"
        echo "Retrying..."
        # You can probably increase this delay
        sleep 10
    fi
done

相关内容