python module for nslookup

空扰寡人 提交于 2019-11-30 17:04:51
PajE

I'm using the following code:

import socket

ip_list = []
ais = socket.getaddrinfo("www.yahoo.com",0,0,0,0)
for result in ais:
  ip_list.append(result[-1][0])
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('dnspython.org', 'MX')
for rdata in answers:
    print 'Host', rdata.exchange, 'has preference', rdata.preference

You should use socket library http://docs.python.org/2/library/socket.html

System function call is not a good practice in this case.

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", "www.google.com"], 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: ',''))
ip_arr.pop(0)

print ip_arr

it will print:

['54.230.228.101', '54.230.228.6', '54.230.228.37', '54.230.228.80', '54.230.228.41', '54.230.228.114', '54.230.228.54', '54.230.228.23']

Note that socket.getfqdn() can return the full-qualified name of a hostname. See: http://docs.python.org/2/library/socket.html?highlight=socket.getaddrinfo#socket.getfqdn

For example:

python -c  'import socket; print(socket.gethostname()); print(socket.getfqdn());'
myserver
myserver.mydomain.local

But the result depends the /etc/hosts configuration. If you have:

$ cat /etc/hosts
127.0.0.1       myserver  localhost.localdomain  localhost

The result of socket.getfqdn() will be:

python -c  'import socket; print(socket.getfqdn());'
localhost.localdomain

Oooops! To solve that, the only solution I know is to change the /etc/hosts as follow:

$ cat /etc/hosts
127.0.0.1       myserver  myserver.mydomain.local  localhost.localdomain  localhost

Hope it helps!

I needed to track down A records in AWS Route 53 using CNames. AKA messaging.myCompany.com to moreSpecificMessaging.myCompanyInternal.com

I also use Socket, but another rather hidden method.

import socket

addr1 = socket.gethostbyname_ex('google.com')

print(addr1)

https://docs.python.org/3/library/socket.html#socket.gethostbyname_ex

Previous answers are correct but here is what I would use socket, it "provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms."

import socket

distinct_ips = []
# 0,0,0,0  is for (family, type, proto, canonname, sockaddr)
#change some_site.com to whatever your are looking up of course
socket_info = socket.getaddrinfo("some_site.com",0,0,0,0)
for result in socket_info:
    ns_ip = result[4][0]
    if distinct_ips.count(ns_ip)==0:
        distinct_ips.append(ns_ip)
        print(ns_ip)

If nslookup or dig are not installed on the system, I use the following for an interactive usage, nslookup-friendly dns lookup output

import socket,argparse

def main(args):
    fqdn = args.fqdn
    try:
        ip_addr = socket.gethostbyname(fqdn)
        server_info()
        print 'Name: ' + fqdn
        print 'Address: ' + ip_addr
    except socket.gaierror:
        server_info()
        print "** server can't find {0}: NXDOMAIN".format(fqdn)

def server_info():
    lines = [line.rstrip('\n') for line in open('/etc/resolv.conf')]
    for line in lines:
        if line.startswith('nameserver'):
            print 'Server:\t\t' + line.split()[1]
            print 'Address:\t' + line.split()[1] + '#53\n'
            break

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Python nslookup utility')
    parser.add_argument('fqdn', type=str, help='Fully qualified domain name')
    args = parser.parse_args()
    main(args)

I'm using the following code:

import socket
addr1 = socket.gethostbyname('google.com')
addr2 = socket.gethostbyname('yahoo.com')
print(addr1, addr2)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!