How to do a batch forward and reserve lookup using dig and host

For this example, I have a list of FQDNs and IPs in two files namely hostnames and ips. We will look at how to do a bulk query using dig and host in 6 examples.

1. Forward lookup using dig in batchmode and return only IPs

dig -f hostnames +short
192.168.1.148
192.168.1.149
192.168.1.150
192.168.1.151
192.168.1.152
192.168.1.153
192.168.1.154
192.168.1.181
192.168.1.182
192.168.1.183
192.168.1.184

2. Forward lookup using dig in batchmode and return A records

dig -f hostnames +noall +answer
rtfmp107.example.com. 479  IN      A       192.168.1.148
rtfmp109.example.com. 5    IN      A       192.168.1.149
rtfmp111.example.com. 900  IN      A       192.168.1.150
rtfmp113.example.com. 804  IN      A       192.168.1.151
rtfmp115.example.com. 804  IN      A       192.168.1.152
rtfmp117.example.com. 186  IN      A       192.168.1.153
rtfmp119.example.com. 5    IN      A       192.168.1.154
rtfmp139.example.com. 900  IN      A       192.168.1.181
rtfmp141.example.com. 4    IN      A       192.168.1.182
rtfmp143.example.com. 1    IN      A       192.168.1.183
rtfmp145.example.com. 1    IN      A       192.168.1.184

3. Bulk reverse lookup with dig and xargs

cat ips | xargs -Ih dig +noall +answer -x  h
148.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp107.example.com.
149.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp109.example.com.
150.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp111.example.com.
151.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp113.example.com.
152.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp115.example.com.
153.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp117.example.com.
154.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp119.example.com.
181.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp139.example.com.
182.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp141.example.com.
183.1.168.192.in-addr.arpa. 900 IN      PTR     rtfmp143.example.com.
184.1.168.192.in-addr.arpa. 352 IN      PTR     rtfmp145.example.com.

4. To extract just the names

cat ips  | xargs -Ih dig +noall +answer -x  h | awk '{print $5}' | sed 's/com./com/g'
rtfmp107.example.com
rtfmp109.example.com
rtfmp111.example.com
rtfmp113.example.com
rtfmp115.example.com
rtfmp117.example.com
rtfmp119.example.com
rtfmp139.example.com
rtfmp141.example.com
rtfmp143.example.com
rtfmp145.example.com

5. Forward lookup with host and xargs for a list of hostnames

cat hostnames | xargs -Ih host h
rtfmp107.example.com has address 192.168.1.148
rtfmp109.example.com has address 192.168.1.149
rtfmp111.example.com has address 192.168.1.150
rtfmp113.example.com has address 192.168.1.151
rtfmp115.example.com has address 192.168.1.152
rtfmp117.example.com has address 192.168.1.153
rtfmp119.example.com has address 192.168.1.154
rtfmp139.example.com has address 192.168.1.181
rtfmp141.example.com has address 192.168.1.182
rtfmp143.example.com has address 192.168.1.183
rtfmp145.example.com has address 192.168.1.184

6. Reserve lookup with host and xargs for a list of hostnames

cat ips | xargs -Ih host h
148.1.168.192.in-addr.arpa domain name pointer rtfmp107.example.com.
149.1.168.192.in-addr.arpa domain name pointer rtfmp109.example.com.
150.1.168.192.in-addr.arpa domain name pointer rtfmp111.example.com.
151.1.168.192.in-addr.arpa domain name pointer rtfmp113.example.com.
152.1.168.192.in-addr.arpa domain name pointer rtfmp115.example.com.
153.1.168.192.in-addr.arpa domain name pointer rtfmp117.example.com.
154.1.168.192.in-addr.arpa domain name pointer rtfmp119.example.com.
181.1.168.192.in-addr.arpa domain name pointer rtfmp139.example.com.
182.1.168.192.in-addr.arpa domain name pointer rtfmp141.example.com.
183.1.168.192.in-addr.arpa domain name pointer rtfmp143.example.com.
184.1.168.192.in-addr.arpa domain name pointer rtfmp145.example.com.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s