-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhostip
More file actions
25 lines (25 loc) · 710 Bytes
/
hostip
File metadata and controls
25 lines (25 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function hostip()
{
declare -i debug
debug=0
while [[ "$1" =~ ^-v+ ]] ; do
verbose_arg=$1
num_verbose=${#verbose_arg[0]}
debug=$((debug + num_verbose - 1))
shift
done
[ $debug -gt 0 ] && echo "Debug level: $debug"
host=$1
if [ -z "${host}" ] || [[ $host =~ ^- ]] ; then
echo "Usage: hostip [-v] hostname"
return 0
fi
output=$(host ${host})
ip=$(echo ${output} | awk '/has address/ && $4 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ { print $4 }')
[ $debug -ge 2 ] && echo "Output of \"host ${host}\": $output"
if [ -n "${ip}" ] ; then
echo ${ip}
else
[ $debug -gt 0 ] && echo "Error: no match"
fi
}