Skip to content

Instantly share code, notes, and snippets.

@jrwarwick
Last active February 23, 2022 20:52
Show Gist options
  • Save jrwarwick/a3f6ce3bd6cc0739597a7f24d45dabf5 to your computer and use it in GitHub Desktop.
Save jrwarwick/a3f6ce3bd6cc0739597a7f24d45dabf5 to your computer and use it in GitHub Desktop.
Emulate nslookup with dig
##Simple, partial, more-or-less emulation of nslookup when all you have is dig
##tested on RHEL 8 and Ubuntu 20. Sometimes local dns cache is reported as SERVER.
nslookup () { dig $([[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && echo "-x" || echo " ") $1 | egrep 'SERVER|^[^;]' | sed -e 's/\.\s*[0-9]*\s*IN\s*/\t/' -e 's/^;; /\tvia DNS /' -e 's/#[0-9]\+(.*//' | cat -s; }
## slightly fancier version, that will definitely be linux only, and one with nmcli on it, but will prevent internal DNS server cache answers.
# nslookup () { dig @$(nmcli | grep servers | cut -d' ' -f 2) $([[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && echo "-x" || echo " ") $1 | egrep 'SERVER|^[^;]' | sed -e 's/\.\s*[0-9]*\s*IN\s*/\t/' -e 's/^;; /\tvia DNS /' -e 's/#[0-9]\+(.*//' | cat -s; }
## and here is a solaris edition which works around various limitations on tab characters
# nslookup () { dig $([[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && echo "-x" || echo " ") $1 | egrep 'SERVER|^[^;]' | sed -e "s/\.[ $(printf '\t')]\{1,\}*[0-9]*[ $(printf '\t')]\{1,\}*IN[ $(printf '\t')]\{1,\}*/$(printf '\t')/" -e "s/^;; /$(printf '\t')via DNS /" -e 's/#[0-9]\{1,\}(.*//' | cat -s; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment