Today's tip is once again focused on Bash functions (I have a whole bunch to share; they're just too useful :-) ). These are three quick and easy functions for performing DNS lookups:
ns
- perform standard resolution of hostnames or IP addresses using nslookup
; only resolved names/addresses are shown in the results
mx
- perform MX record lookup to determine mail servers (and priority) for a particular domain
mxip
- perform MX record lookup, but return mail server IP addresses instead of host names
Here are the functions:
# Domain and MX record lookups
# $1 = hostname, domain name, or IP address
function ns() {
nslookup $1 | tail -n +4 | sed -e 's/^Address:[[:space:]]\+//;t;' -e 's/^.*name = \(.*\)\.$/\1/;t;d;'
}
function mx() {
nslookup -type=mx $1 | grep 'exchanger' | sed 's/^.* exchanger = //'
}
function mxip() {
nslookup -type=mx $1 | grep 'exchanger' | awk '{ print $NF }' | nslookup 2>/dev/null | grep -A1 '^Name:' | sed 's/^Address:[[:space:]]\+//;t;d;'
}
And finally, some examples:
$ ns mail.legroom.net # forward lookup
64.182.149.164
$ ns 64.182.149.164 # reverse lookup
mail.legroom.net
$ ns www.legroom.net # cname example
legroom.net
64.182.149.164
$ mx legroom.net # mx lookup
10 mail.legroom.net.
$ mxip legroom.net # mx->ip lookup
64.182.149.164