Python实现自动化域名批量解析
Python实现自动化域名批量解析
别人的知识,自己学会后,就是自己的,学无止境
脚本架构
domain_test.py:批量解析运行主程序
DomainResult.txt:域名解析结果文件
domains.txt:解析的域名文件
实现代码如下:
# coding:utf-8import socketimport subprocessimport redef get_host_from_file(file_path): with open(file_path, 'r') as fr: domains = fr.readlines() result = [] for url in domains: url = url.strip() try: ips = socket.gethostbyname_ex(url)[-1] result.append(url + '\t' + ';'.join(ips) + '\t' + 'ping' + '\n') except Exception as e: print(url, e) with open('./domain2ip.txt', 'w') as fw: fw.writelines(result)def get_host_from_url(url): try: ips = socket.gethostbyname_ex(url)[-1] return url + '\t' + ';'.join(ips) + '\t' + 'ping' + '\n' except Exception as e: print(url, e) return url + '\t' + 'none' + '\n'def dig_test(file_name, dns_name): dig_command = 'dig ' ip_result = [] if dns_name: dig_command += dns_name + ' ' with open(file_name) as fr: domains = fr.readlines() for ui, full_url in enumerate(domains): ips = [] full_url = full_url.strip() try: result = subprocess.Popen(dig_command + full_url, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) except Exception as e: print(full_url, e) else: results = str(result.stdout.read()).split('\\n') for temp in results: if full_url in temp and 'IN' in temp: ip = re.match(r'.*\\t([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*', temp) if ip and ip.group(1) not in ips: ips.append(ip.group(1)) if 'AUTHORITY SECTION' in temp: break if ips: temp = full_url + '\t' + ';'.join(ips) + '\t' + 'dig' + '\n' else: temp = get_host_from_url(full_url) print(ui, temp) ip_result.append(temp)#解析完成后,生成结果文件 with open('domains.txt', 'w') as fw: fw.writelines(ip_result)if __name__ == '__main__': # 先使用dig,失败时使用ping获取域名ip,可指定dns,如@114.114.114.114 dig_test(file_name='DomainResults.txt', dns_name='')
演示结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~