Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Last active December 10, 2015 08:38
Show Gist options
  • Select an option

  • Save stefanocudini/4408724 to your computer and use it in GitHub Desktop.

Select an option

Save stefanocudini/4408724 to your computer and use it in GitHub Desktop.
ping for multiple hosts, check /etc/hosts is online
#!/bin/sh
HOSTS=/etc/hosts
MEHOST=$(hostname)
DNSHOST=$(cut -d' ' -f2 /etc/resolv.conf)
GWHOST=$(ip route | grep default | cut -d' ' -f3)
IGNORE="^$\|^#\|^127\|$MEHOST\|$DNSHOST"
#echo $IGNORE
HOSTS=$(grep -v $IGNORE $HOSTS | tr '\t' ' ' | cut -d' ' -f1 | sort | uniq)
for H in $HOSTS; do
ping -q -c1 -w1 $H >/dev/null 2>&1
if [ "$?" -eq 0 ]; then
echo $H
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment