Python 处理多DNS的nslookup批量解析

概述

  • 处理多DNS批量解析nslookup

  • 输入文件格式为:

  • 输出文档格式为:

  • Usage:
    python3 domain.py

# coding=utf-8
import subprocess
import csv


def get_nslookup(domain, dns):
    res = subprocess.Popen("nslookup {0} {1}".format(domain, dns), stdin=subprocess.PIPE,
                           stdout=subprocess.PIPE).communicate()[0]
    response = res.decode("utf-8", "ignore")
    res_list = response.split("s:")
    row_nslookup = [domain, dns]
    row_ip = res_list[2].split()[:-1]
    row_nslookup.extend(row_ip)
    return row_nslookup


if __name__ == "__main__":
    file_domain = r'.\domain.csv'  # 输入域名列表文件
    file_nslookup = r'.\nslookup1.csv'  # 输出解析结果
    with open(file_domain, 'r', newline='', encoding='utf-8') as rf:
        domain_csv = csv.DictReader(rf, dialect=csv.excel)
        domain_list = [row['domain'] for row in domain_csv]

    with open(file_domain, 'r', newline='', encoding='utf-8') as rf:
        domain_csv = csv.DictReader(rf, dialect=csv.excel)
        dns_list = []
        for row in domain_csv:
            print(row['DNS'])
            if row['DNS'] != '':
                dns_list.append(row['DNS'])

    with open(file_nslookup, 'w+', newline='', encoding='utf-8') as wf:
        nslookup_csv = csv.writer(wf, dialect=csv.excel)
        header = ['domain', 'DNS', 'nslookup_res...']
        nslookup_csv.writerow(header)
        for domain in domain_list:
            for dns in dns_list:
                #异常处理
                try:
                    print('解析中:域名:{0}_DNS:{1}'.format(domain, dns))
                    row_nslookup = get_nslookup(domain, dns)
                    nslookup_csv.writerow(row_nslookup)
                except Exception as e:
                    print("error")

print('执行完毕')

posted @ 2020-11-05 13:58  g7y12  阅读(1896)  评论(0编辑  收藏  举报