How to find out IP addresses for a list of hostnames?

Python for network scripting

As you all know python is a generic language, it can be used in networking field as well. May the below python script will be great time saver for network ninjas😎.

Here we are using a python package called socket. This package is used for socket programming. The given script will read a set of host names line by line from a file to a list. Then it will resolve all the host names and will display the IP address of the corresponding host name.


Example:
First we will create a text file host.txt with some host names to resolve. Here I will be using some domain names like www.google.com, www.youtube.com, www.vicz.in, blog.vicz.in. You can also use the same as an example. These domain names you should write line by line in the host.txt.

import socket
hostnames=open('host.txt').readline()
for host in hostnames:
    print host.strip(),socket.gethostbyname(host.strip())
output:
www.google.com 216.58.203.228
www.youtube.com 74.125.200.190
www.vicz.in 104.18.46.75
blog.vicz.in 104.18.46.75
Screenshot:

 

 

Here you can find out more network scripts using python https://github.com/vishnu-ks/Python-networking-scripts



Comments

Popular Posts