Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. I'm a total n00b when it comes to this, so my code is pretty harsh. 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.

share|improve this question
    
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
    
Thank you G-Man, it wasn't for any good reason. – P Toscano Dec 15 '15 at 23:00
    
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
up vote 1 down vote accepted

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
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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