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.

3
  • What kind of timeout did you use?
    – choroba
    Commented Dec 15, 2015 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. Commented Dec 15, 2015 at 22:01
  • choroba I was using timeout, but for the entire script, not one device, I couldn't figure that out.
    – P Toscano
    Commented Dec 15, 2015 at 23:01

2 Answers 2

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.