Skip to content

Instantly share code, notes, and snippets.

@jameswebb68
Created October 26, 2014 18:47
Show Gist options
  • Save jameswebb68/3afd6bc9a880e769453d to your computer and use it in GitHub Desktop.
Save jameswebb68/3afd6bc9a880e769453d to your computer and use it in GitHub Desktop.
bind-utils host on cygwin not piping stdout correctly fix
# temporary fix for Cygwin 1.7.32 - 1.7.33+ and bind-utils
# rolls windows builtin nslookup into bash function
# outputs exactly how bind-utils output so that host calls in other places don't break
# source this file, roll your own, or insert into existing shell scripts
# quick and dirty, but effective, untested on zsh and ash
# Jon Retting @ 10/26/2014
#TEMP host.exe fix for output
# check to make sure ip is valid
__isip () {
printf "$1" | grep -Eq '^[1-9]{1}[0-9]{0,2}\.[1-9]{1}[0-9]{0,2}\.[1-9]{1}[0-9]{0,2}\.[1-9]{1}[0-9]{0,2}' || return 1
for i in $(printf "$1" | sed 's/\./\ /g'); do
[ "$i" -ge 255 ] && return 1
done
return 0
}
host () {
# determine if passed host is alphanumeric else verify ip address
printf "$1" | grep -Eq '[[:alnum:]]' && { local alpha=true; } || {
__isip "$1" || { echo "IP not valid"; return 1; }
local alpha=false
}
# get windows nslookup output
local out="$(/cygdrive/c/Windows/SYSTEM32/nslookup "$1" 2>&1)"
if $alpha; then
# if is alphamumeric and dns does not exist
if printf "$out" | grep -q 'Non-existent'; then
# output to match bind-utils hosts
printf "%sHost $1 not found: 3(NXDOMAIN)\n"; return 1
else
# if is alphanumeric and dns exists
local host="$(printf "$out" | awk '/Name/ {print $2}')" #grab the host name
local ip="$(printf "$out" | awk '/Address/ {print $2}' | sed 1d)" #grab the ip address
printf "%s$host has address $ip\n" #output to match bind-utils hosts
return 0
fi
else
# if is ip address and dns does not exist
if printf "$out" | grep -q 'Non-existent'; then
# output to match bind-utils hosts
printf "%sHost $(printf $1 | rev).in-addr.arpa. not found: 3(NXDOMAIN)\n"
return 1
else
# if is ipaddress and dns exists
local host="$(printf "$out" | awk '/Name/ {print $2}')." #grab the host name
local ip="$(printf "$out" | awk '/Address/ {print $2}' | sed 1d | rev)." #grab the ip address
printf "%s$ip in-addr.arpa domain name pointer $host\n" #output to match bind-utils hosts
return 0
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment