1

I'm writing a script that SSH into a device, SCP a file over, names it according to the device name and then goes on to the next one. My problem is if a device is not reachable the script hangs forever. Here's my code:

#!/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

It works as is, but the timeout I used canceled the script as a whole, not skipping to the next device. Any ideas would be greatly appreciated.

  • What kind of timeout did you use? – choroba Dec 15 '15 at 21:37
  • This is not directly related to your problem, but you should always quote your shell variable references (e.g., "/mnt/hgfs/Ubiquiti/$one.cfg" and (as shown in Gilles Quenot’s answer) echo "$one Success!") unless you have a good reason not to, and you’re sure you know what you’re doing.  And, yes, it would be helpful if you showed us the timeout usage that you’re having trouble with. – G-Man Dec 15 '15 at 22:01
  • choroba I was using timeout, but for the entire script, not one device, I couldn't figure that out. – P Toscano Dec 15 '15 at 23:01
2

Try timeout command like this :

#!/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
1

Thank you all. I am satisfied with the script and it performs exactly as intended.

#!/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.