Using Timeout in a Script with One Command, but Multiple Hosts

7,518

Solution 1

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

Solution 2

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:
7,518

Related videos on Youtube

P Toscano
Author by

P Toscano

Updated on September 18, 2022

Comments

  • P Toscano
    P Toscano almost 2 years

    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.

    • G-Man Says 'Reinstate Monica'
      G-Man Says 'Reinstate Monica' over 8 years
      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.
    • P Toscano
      P Toscano over 8 years
      choroba I was using timeout, but for the entire script, not one device, I couldn't figure that out.