Python實現(xiàn)自動化域名批量解析分享
更新時間:2022年08月01日 11:21:17 作者:愛搞網(wǎng)絡(luò)的皮卡丘???????
這篇文章主要介紹了Python實現(xiàn)自動化域名批量解析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
腳本架構(gòu):

- domain_test.py:批量解析運行主程序
- DomainResult.txt:域名解析結(jié)果文件
- domains.txt:解析的域名文件
實現(xiàn)代碼如下:
# coding:utf-8
import socket
import subprocess
import re
def 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)
#解析完成后,生成結(jié)果文件
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='')演示結(jié)果:

到此這篇關(guān)于Python實現(xiàn)自動化域名批量解析的文章就介紹到這了,更多相關(guān)Python自動化域名解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中遍歷字典過程中更改元素導(dǎo)致異常的解決方法
這篇文章主要介紹了Python中遍歷字典過程中更改元素導(dǎo)致錯誤的解決方法,針對增刪元素后出現(xiàn)dictionary changed size during iteration的異常解決做出討論和解決,需要的朋友可以參考下2016-05-05
10 行Python 代碼實現(xiàn) AI 目標(biāo)檢測技術(shù)【推薦】
這篇文章主要介紹了10 行Python 代碼,實現(xiàn) AI 目標(biāo)檢測技術(shù),看完了代碼,我們在一起聊聊目標(biāo)檢測背后的技術(shù)背景,并解讀這10行Python代碼的由來和實現(xiàn)原理。感興趣的朋友跟隨小編一起看看吧2019-06-06
淺談Python數(shù)學(xué)建模之?dāng)?shù)據(jù)導(dǎo)入
數(shù)據(jù)導(dǎo)入是所有數(shù)模編程的第一步,比你想象的更重要。Python 語言中數(shù)據(jù)導(dǎo)入的方法很多。對于數(shù)學(xué)建模問題編程來說,選擇什么方法最好呢?答案是:沒有最好的,只有最合適的。對于不同的問題,不同的算法,以及所調(diào)用工具包的不同實現(xiàn)方法,對于數(shù)據(jù)就會有不同的要求2021-06-06

