I have a Bash script that uses SSH keys to login to a list of servers.
The script iterates over a flat text file of hostnames called test_hosts.txt.
The script works fine except if one of the logins fails for some reason. If a login fails on one of the hosts, the entire script stops and none of the other hosts on the list are logged into.
How can I adjust this script to keep going if the login fails for any reason?
#!/bin/bash
USER="username@domain@"
KPATH="/path/to/key"
while read host; do
echo $host
echo ${KPATH}
GETSIZE="df -h"
ssh ${USER}$host ${KPATH} "${GETSIZE}" > Test/Hosts/$host.txt
done <Test/Hosts/test_hosts.txt
UPDATE: The script stops meaning, it does not continue on to the next host on the list.
This is a guess, but ssh
tends to read from standard input, and try to pass whatever it finds there to the remote computer. When you use ssh
in a while read
loop, this can mean that ssh
will steal the data you intended to read
in the loop. If this is the problem, there's a fairly simple solution: pass the host file over fd 3 instead of standard input:
while read -u3 host; do
...
done 3<Test/Hosts/test_hosts.txt
(The 3<
opens the file on fd 3 instead of stdin, and -u3
tells read
to read from fd 3.)
Another possibility is to add the -n
option to ssh
, which tells it not to read from stdin.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments