Skip to content

Instantly share code, notes, and snippets.

@slushpupie
Created December 5, 2014 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slushpupie/6194147504684534008e to your computer and use it in GitHub Desktop.
Save slushpupie/6194147504684534008e to your computer and use it in GitHub Desktop.
# A simple function to blow up the list of hosts (by A record only, no CNAMEs)
# and spit them out in a simple pair to be consumed by other scripts. If the
# name given has no A records, nothing will output. If an A record has no
# corresponding PTR, the IP will be placed in the name field.
name_expand() {
if [ -z "$1" ] ; then
echo usage: name_expand hostname
return 1
fi
ip_list=$(dig +short $1 A | grep -v '^;')
for ip in $ip_list ; do
host=$(dig +short -x $ip)
if [ -z "$host" ] ; then
host=$ip
fi
echo "$host $ip"
done
}
# Uses name_expand, and adds a ping Round-Trip time (single ping) for each line
ping_expand() {
if [ -z "$1" ] ; then
echo usage: ping_expand hostname
return 1
fi
# Expand name into a list of IPs
ip_list=$(name_expand $1)
OLD_IFS=$IFS
IFS=$'\n'
for line in $ip_list ; do
ip=$(echo $line | cut -f 2 -d ' ')
rtt=$(ping -q -c 1 $ip | grep '^round-trip' | cut -d ' ' -f 4 | cut -d '/' -f 1)
echo "$line $rtt"
done
IFS=$OLD_IFS
}
# Ping a list of hosts
ping_list() {
hosts=$*
for host in $hosts ; do
rtt=$(ping -q -c 1 $host | grep '^round-trip' | cut -d ' ' -f 4 | cut -d '/' -f 1)
echo "$host $rtt"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment