I'm trying to connect to multiple hosts to check config files for various things like ldap and dns servers being used. My end goal is to just give it a list of hosts (preferably a file outside of the script), and whatever $SCRIPT I want it to run, and dump it to a file to check for errors. It connects to the first host fine, outputs to file fine, but then just stops, and won't connect to subsequent hosts, namely, app-03.
#!/bin/bash
HOSTS="app-01.stage app-03.stage"
SCRIPT1="hostname"
SCRIPT2="grep ldp-02 /etc/ldap.conf"
SCRIPT3="cat /etc/resolv.conf"
FILE="test.txt"
for HOSTNAME in ${HOSTS} ;
do
ssh -o "StrictHostKeyChecking no" -n $HOSTNAME "$SCRIPT1; $SCRIPT2; $SCRIPT3"
if [ "$?" = 1 ]; then
echo "FAIL - could not connect"
else
exit
fi
done >> $FILE