Script Architecture:
- domain_test.py:Batch parsing to run the main program
- :Domain name resolution result file
- :Resolved domain name files
The realization code is as follows:
# coding:utf-8 import socket import subprocess import re def get_host_from_file(file_path): with open(file_path, 'r') as fr: domains = () result = [] for url in domains: url = () try: ips = socket.gethostbyname_ex(url)[-1] (url + '\t' + ';'.join(ips) + '\t' + 'ping' + '\n') except Exception as e: print(url, e) with open('./', 'w') as fw: (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 = () for ui, full_url in enumerate(domains): ips = [] full_url = full_url.strip() try: result = (dig_command + full_url, shell=True, stdout=, stderr=) except Exception as e: print(full_url, e) else: results = str(()).split('\\n') for temp in results: if full_url in temp and 'IN' in temp: ip = (r'.*\\t([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*', temp) if ip and (1) not in ips: ((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) # Generate result file after parsing is complete with open('', 'w') as fw: (ip_result) if __name__ == '__main__': # Use dig first, when it fails use ping to get domain ip, can specify dns, as [email protected] dig_test(file_name='', dns_name='')
Demo results:
This article on Python automated domain name batch resolution is introduced to this article, more related Python automated domain name resolution content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!