After the success view of the host ( IPs ) I need to ping them in order to check if they are up. SIDS file contains 2 columns with hostnames. Are there any suggestions on how to Iimprove the code below?
#!/bin/bash
LINES=`cat /home/marko/SIDS | sed "s!/!-!g" | wc -l`
for (( i=1; i<=${LINES}; i++))
do
FIRSTIP=CPE-`sed -n "${i}{p;q}" /home/marko/SIDS | awk '{print $1}'| sed "s!/!-!g"`
SECONDIP=CPE-`sed -n "${i}{p;q}" /home/marko/SIDS | awk '{print $2}'| sed "s!/!-!g"`
COUNT=$( host ${FIRSTIP} | grep address | wc -l )
if [ $COUNT -gt 0 ]
then
echo success
else
echo ${SECONDIP}
fi
done
You can just use dig
, to avoid searching the output of host
:
IP=$(dig +short $SERVERNAME)
Then to check, if the host is alive:
if ping -q -c $IP >/dev/null 2>&1
then
echo "OK"
fi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.