Wednesday 26 October 2016

One liner for a more readable nslookup output

nslookup is a common command run to verify host name resolution. If we need to check information for a couple of systems, it' s easy.
If we need to validate the details for hundreds of servers then we can run the for loop over the host names and provide the information but it won't be very readable as it'll contains the name of DNS nameserver in every iteration of the loop.
So I created a quick one liner to produce a neat (relatively neat) output.

So, I put the host names in a file named pt2dns like below:

#cat pt2dns
test27367-bkp
test27368-bkp
test27369-bkp
test27370-bkp
test27371-bkp
test27372-bkp
test27373-bkp
test27374-bkp
test27375-bkp
-------------
-------------

Then I ran the below one liner:

for i in $(<pt2dns) ; do echo "server name " `nslookup  $i | awk  '/example/ {print $2}'` "has an IP address" `nslookup  $i | awk  '/172/ {print $2}'` ; done


I did an awk pattern search for the domain name the first time & the subnet name in the second awk pattern search.

This produces the following output:

server name  test27367-bkp.example.com.in has an IP address 172.27.27.202
server name  test27368-bkp.example.com.in has an IP address 172.27.27.203
server name  test27371-bkp.example.com.in has an IP address 172.27.27.204
server name  test27372-bkp.example.com.in has an IP address 172.27.27.205
server name  test27373-bkp.example.com.in has an IP address 172.27.27.206
server name  test27374-bkp.example.com.in has an IP address 172.27.27.207

This is definitely easier to read & copy out in a file/excel work sheet compared to the normal nslookup output.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...