Bottom Page

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)

Quote
#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)

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  random two-word domain name generator rootVIII 0 250 Aug-06-2019, 03:15 AM
Last Post: rootVIII
  get any domain's Alexa rank via command line rootVIII 2 717 Oct-14-2018, 08:34 PM
Last Post: rootVIII
  a class that gets available, running, and stopped services (Ubuntu/Debian) rootVIII 5 943 Aug-26-2018, 03:15 PM
Last Post: rootVIII

Forum Jump:


Users browsing this thread: 1 Guest(s)