python3 批量处理域名解析

  • 域名批量解析,快速确认域名的存活性及IP地址,脚本中包含了具体的用法和简要说明
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# python3.6

from socket import gethostbyname
import argparse

def domain_ip(openfile,out1,out2,out3):
    with open(openfile, 'r') as f:
        for line in f.readlines():
            try:
                host = gethostbyname(line.strip('\n'))  # 域名反解析得到的IP
            except Exception as e:
                with open(out1, 'a+') as ERR:  # A-domain-ERR.txt 解析出错域名统计
                    ERR.write(line.strip() + '\n')
            else:
                with open(out2, 'a+') as r:  # A-domain-ip.txt 里面存储的是批量解析后的结果,输出域名
                    r.write(line.strip('\n') + '  '+ host +"\n")  # 显示有ip绑定的域名,用空格隔开
                    re = line.strip('\n')
                    print(re,host)
					#不包含的解析结果
                    if host.strip() != "127.0.0.0":
                        if host.strip()!= "192.168.1.1":
                            if host.strip() != "192.168.2.1":
                                if host.strip() != "192.168.3.1":
                                    with open(out3,"a+") as f:
					f.write(host.strip()+"\n")
                                        #f.write(line.strip()+"  " + host.strip()+"\n")
                                else:
                                    pass
                            else:
                                pass
                        else:
                            pass
                    else:
                        pass

if __name__== "__main__":
    parser = argparse.ArgumentParser(description="2020.07.28 python 3.6 domain_1.0")
    parser.add_argument('-f'.strip(),'--filename'.strip(), help='eg:-f domains.txt')# 需要解析的域名列表信息
    parser.add_argument('-o'.strip(),'--outfile1'.strip(), default="A-domain-ERR.txt",help='eg:-o A-domain-ERR.txt ')# 输出解析失败的域名信息
    parser.add_argument('-r'.strip(),'--outfile2'.strip(), default="A-domain-ip.txt",help='eg:-r A-domain-ip.txt ')#输出解析成功的域名及ip信息
    parser.add_argument('-a'.strip(), '--outfile3'.strip(), default="A-lists.txt",help='eg:-a A-lists.txt ')#输出A记录列表
    args = parser.parse_args()
    if (args.filename and args.outfile1 and args.outfile2 and args.outfile3):
        domain_ip(args.filename, args.outfile1,args.outfile2,args.outfile3)
    else:
        print(parser.format_help())

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