ahz10 |
08-23-2006 11:44 AM |
A script to thoroughly resolve a hostname into IP addresses
Since sometimes `dig example.com +short` returns more hostnames, here's a script to thoroughly resolve all the results into only IP addresses.
Code:
#!/bin/bash
/**
* paramater 1: a string
* return: 0 hostname
* return: 1 ip address
*/
function is_ip_address()
{
R=`echo $1 | egrep "[a-z]"`
r=$?
if [ -z "$R" ]
then
return 1
fi
return 0
}
#
# parameter 1: a hostname to resolve
# side effects: echos IP addresses (which may not be unique)
function resolve_hostname()
{
is_ip_address $1
if [[ $? -eq 1 ]]
then
echo "$1"
return
fi
for a in `dig $1 +short`
do
resolve_hostname $a
done
}
# general way to call the function
resolve_hostname "www.concentric.net"
# you can also assign the output to a variable
# and make the results unique
r=`resolve_hostname "real.npr.org" | sort |uniq`
echo $r
|