Python Forum
A small/simple class for running nslookup on a list of domain names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A small/simple class for running nslookup on a list of domain names
#1
might make it return more stuff in the future... here it is for now:

#! /usr/bin/python3
from subprocess import Popen, PIPE
from re import findall


class NSLookup:
    def __init__(self, domains):
        self.domains = domains
        self.canonical = r"\s+canonical\s+name\s+=\s+(.*)\s+"
        self.address = r"Address:\s+(\d+.\d+.\d+.\d+)\s+"

    def examine(self):
        for d in self.domains:
            data = {'domain': d, 'names': [], 'ips': []}
            cmd = ["nslookup",  d]
            out = Popen(cmd, stdout=PIPE).communicate()[0].decode()
            server_names = findall(self.canonical, out)
            server_ips = findall(self.address, out)
            for name in server_names:
                data['names'].append(name)
            for ip in server_ips:
                data['ips'].append(ip)
            yield data


if __name__ == "__main__":
    # EXAMPLE CLIENT:
    domain_list = [
        'www.unc.edu', 'www.umb.edu', 'www.harvard.edu',
        'www.cornell.edu', 'www.psu.edu', 'www.cam.ac.uk',
        'www.umass.edu', 'www.mit.edu', 'www.unimelb.edu.au'
    ]
    for test in NSLookup(domain_list).examine():
        print(test)
Reply
#2
EDIT:

Replaced those two for loops with list comprehensions:

#! /usr/bin/python3
from subprocess import Popen, PIPE
from re import findall


class NSLookup:
    def __init__(self, domains):
        self.domains = domains
        self.canonical = r"\s+canonical\s+name\s+=\s+(.*)\s+"
        self.address = r"Address:\s+(\d+.\d+.\d+.\d+)\s+"

    def examine(self):
        for d in self.domains:
            data = {'domain': d, 'names': [], 'ips': []}
            cmd = ["nslookup",  d]
            out = Popen(cmd, stdout=PIPE).communicate()[0].decode()
            server_names = findall(self.canonical, out)
            server_ips = findall(self.address, out)
            data['names'] = [name for name in server_names]
            data['ips'] = [ip for ip in server_ips]
            yield data


if __name__ == "__main__":
    # EXAMPLE CLIENT:
    domain_list = [
        'www.unc.edu', 'www.umb.edu', 'www.harvard.edu',
        'www.cornell.edu', 'www.psu.edu', 'www.cam.ac.uk',
        'www.umass.edu', 'www.mit.edu', 'www.unimelb.edu.au'
    ]
    for test in NSLookup(domain_list).examine():
        print(test)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Small programm to change names of files Brotato 0 1,741 Aug-20-2020, 08:12 PM
Last Post: Brotato
  a class that gets available, running, and stopped services (Ubuntu/Debian) rootVIII 5 3,715 Aug-26-2018, 03:15 PM
Last Post: rootVIII

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020