![使用 ssh 和 scp 时出现“丢失连接”错误](https://linux22.com/image/1312063/%E4%BD%BF%E7%94%A8%20ssh%20%E5%92%8C%20scp%20%E6%97%B6%E5%87%BA%E7%8E%B0%E2%80%9C%E4%B8%A2%E5%A4%B1%E8%BF%9E%E6%8E%A5%E2%80%9D%E9%94%99%E8%AF%AF.png)
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