Bash
通過一個命令但多個主機在腳本中使用超時
我正在編寫一個腳本,將 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
它按原樣工作,但我使用的超時取消了整個腳本,而不是跳到下一個設備。任何想法將不勝感激。
試試
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
謝謝你們。我對腳本很滿意,它的表現完全符合預期。
#!/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