Skip to content

Instantly share code, notes, and snippets.

@klang
Created February 26, 2018 07:47
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 klang/d00cf3f5b649d2356907ef156a3c468f to your computer and use it in GitHub Desktop.
Save klang/d00cf3f5b649d2356907ef156a3c468f to your computer and use it in GitHub Desktop.
resolve dns via local hosts file
function resolve {
hostfile=~/.hosts
if [[ -f "$hostfile" ]]; then
for arg in $(seq 1 $#); do
if [[ "${!arg:0:1}" != "-" ]]; then
ip=$(sed -n -e "/^\s*\(\#.*\|\)$/d" -e "/\<${!arg}\>/{s;^\s*\(\S*\)\s*.*$;\1;p;q}" "$hostfile")
if [[ -n "$ip" ]]; then
command "${FUNCNAME[1]}" "${@:1:$(($arg-1))}" "$ip" "${@:$(($arg+1)):$#}"
return
fi
fi
done
fi
command "${FUNCNAME[1]}" "$@"
}
function ping {
resolve "$@"
}
function traceroute {
resolve "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment