Generate a mapping of IP addresses to hostnames and vice-versa in seconds!

As network engineers, most of us have encountered a situation like the one I have described. This is a conversation with an Application guy (let’s call him Bob):

Me: Hey Bob, we are moving Vlan 331 of your SAP application next week and all those servers will be impacted.
Bob: Ok. Which all servers are a part of this Vlan thing?

Me: Well, they are app servers. Shouldn’t you be knowing all your servers?
Bob: Duh! I know my applications, man. Don’t ask me about servers and stuff. You folks manage that.

This is the point in the conversation when you realize that that only person who has to get this info is you! With hesitation, you login to your Layer-3 switch which has the gateway configured for Vlan 331 and run a ‘show ip arp vlan 331’, copy the list of IP addresses and send it to Bob. The conversation then goes something like this:

Me: Hey Bob, did you see my email containing the list of all IPs of your application?
Bob: Yeah, I saw a bunch of numbers separated with dots. That means nothing to me. Just send me a list of all the device hostnames.

You now recall that Vlan 331 is a /22 there are some 1000 plus IPs in that ARP dump. The only way to get this hostname is to run ‘nslookup’ against each of those IPs. Nightmarish scenario and the lazy/smart person inside you should not not permit that. This is the point where you don your sysadmin/devops hat and login to a bash shell.

Below is a simple, yet elegant way to convert all these IPs to their corresponding hostnames in a matter of seconds:

1. Create a new file on a Linux bash shell and call it something like
2. Inside that file, enter the following script:

To all the folks who are unfamiliar with Bash scripting, worry not! We started the script with this thing called as ‘shebang’. This basically tells the system that it a Bash script and /bin/sh is the path for bash. The ‘cat’ command reads through the list of IP addresses from the text file (which we will create later) and the ‘while’ command iterates over all of them line by line. Next, we run the ‘host’ command over each IP which queries the DNS server for an IP to hostname mapping. The ‘awk’ command prints the fifth block of text from the output, which is the hostname. The ‘sed’ command simply removes the annoying dot at the very end of the output.

After the script is saved, create a new file and call it ip_addresses.txt in the same directory as the Bash script. Paste the ARP dump of all the IP addresses line by line. Make sure you just copy the IP addresses and not the other data which is a part of the ARP dump. You can use a tool like Notepad++ for that.

Viola! It’s now time to run our powerful script. On the bash shell, run

Running this command will print the corresponding hostnames of all the IP addresses in the ip_addresses.txt file on the console. It will be more useful to save it in a new file which can be easily exported.

Now, you have a file containing all the hostnames in Vlan 331. Send this file to Bob and everyone lives happily ever after! Well, maybe not.

Furthermore, you can just as easily accomplish the converse as well i.e. create a mapping of hostnames to IP addresses. For whatever reason, if you had a list of hostnames and wanted to get their corresponding IPs, create a new Bash script like this and call it something like

Save the hostnames.txt file in the same directory and run the Bash script with > filename.txt. The newly created file will contain a list all the corresponding IP addresses.

This is just an example of how basic knowledge of scripting and automation can save us hours of time and make us better engineers. All comments/criticisms are welcome!

Leave a Reply

Your email address will not be published. Required fields are marked *