python module for nslookup

python module for nslookup

Im using the following code:

import socket

ip_list = []
ais = socket.getaddrinfo(,0,0,0,0)
for result in ais:
ip_list = list(set(ip_list))

Or using a comprehension as:

ip_list = list({addr[-1][0] for addr in socket.getaddrinfo(name, 0, 0, 0, 0)})

You need to use DNSPython

import dns.resolver

answers = dns.resolver.query(, MX)
for rdata in answers:
    print Host,, has preference, rdata.preference

python module for nslookup

the problem is that socket.gethostbyname() returns only one ip-address. nslookup returns as many as it has.
I use:

import subprocess

process = subprocess.Popen([nslookup,], stdout=subprocess.PIPE)
output = process.communicate()[0].split(n)

ip_arr = []
for data in output:
    if Address in data:
        ip_arr.append(data.replace(Address: ,))

print ip_arr

it will print:


Leave a Reply

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