我正在编写一个脚本,通过 SSH 连接到设备,通过 SCP 传输文件,根据设备名称对其进行命名,然后继续处理下一个文件。我的问题是,如果设备无法访问,脚本将永远挂起。这是我的代码:
#!/bin/bash
echo "Starting Backup of Ubiquiti Radios"
#cut -d' ' -f2 /usr/tmp/Script/Links.csv
#cut -d' ' -f1 /usr/tmp/Script/Links.csv
while read one two; do
if sshpass -p 'supersecretpassword' scp -o StrictHostKeyChecking=no control@"$two":/tmp/system.cfg /mnt/hgfs/Ubiquiti/$one.cfg; then
echo $one Success!
else
echo $one Failed
fi
done < /usr/tmp/Script/Links.csv
它按原样工作,但我使用的超时取消了整个脚本,而不是跳到下一个设备。任何想法将不胜感激。
答案1
尝试timeout
这样的命令:
#!/bin/bash
echo "Starting Backup of Ubiquiti Radios"
while read one two; do
if timeout 60 sshpass -p 'supersecretpassword' scp -o StrictHostKeyChecking=no control@"$two":/tmp/system.cfg /mnt/hgfs/Ubiquiti/$one.cfg; then
echo "$one Success!"
else
echo "$one Failed"
fi
done < /usr/tmp/Script/Links.csv
答案2
谢谢你们。我对脚本很满意,它的表现完全符合预期。
#!/bin/bash
echo "Starting Backup of Ubiquiti Radios"
mkdir "/mnt/hgfs/Ubiquiti/$(date +%Y%m%d)"
while read one two; do
if timeout 10 sshpass -p 'pass' scp -o StrictHostKeyChecking=no control@"$two":/tmp/system.cfg "/mnt/hgfs/Ubiquiti/$(date +%Y%m%d)/$one.cfg"; then
echo "$one Success!"
else
echo "$one Failed"
fi
done < /home/osboxes/Documents/Script/Links.csv