Skip to main content
Home
Legroom.net
User account menu
  • Log in

Breadcrumb

  1. Home

Quick Domain Name / IP Address / MX Record Lookup Functions

By jbreland , 7 May 2010
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

Navigation

  • Home
  • Miscellaneous
  • Software
  • Tips and Tricks
Color theme

All logos, trademarks, and comments on this site are property of their respective owner. 
All other content is ©2025 by Jared Breland and licensed under CC BY-SA 4.0. 
This website is syndicated with an RSS 2.0 newsfeed.  Syndicate content