I need to make my script iterate through a list of servers, but I can't.
Suppose that 192.168.0.1
and 192.168.0.2
are listed in the file that will be read by the script, when executing it only the first line (192.168.0.1
) is successfully processed and nothing happens with the second line (192.168.0.2
).
This is the script:
#!/bin/bash
# Read in server names from afile
while read -r server; do
# Check if server is reachable
if ping -c 1 "$server" &> /dev/nul l; then
echo "Server $server is reachable."
# Check if mount point exists
if ssh "$server" "[ -d /path/to/mount/point ]"; then
echo "Mount point exists on server $server."
else
echo "Mount point does not exist on server $server."
fi
else
echo "Server $server is not reachable."
fi
done < servers.txt
How to go through the entire list of servers successfully?