Python實(shí)現(xiàn)自動(dòng)化域名批量解析分享
腳本架構(gòu):

- domain_test.py:批量解析運(yùn)行主程序
- DomainResult.txt:域名解析結(jié)果文件
- domains.txt:解析的域名文件
實(shí)現(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,失敗時(shí)使用ping獲取域名ip,可指定dns,如@114.114.114.114
dig_test(file_name='DomainResults.txt', dns_name='')演示結(jié)果:

到此這篇關(guān)于Python實(shí)現(xiàn)自動(dòng)化域名批量解析的文章就介紹到這了,更多相關(guān)Python自動(dòng)化域名解析內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 教你在Excel中調(diào)用Python腳本實(shí)現(xiàn)數(shù)據(jù)自動(dòng)化處理的方法
- Python自動(dòng)化辦公之圖片轉(zhuǎn)PDF的實(shí)現(xiàn)
- python和Appium移動(dòng)端多設(shè)備自動(dòng)化測(cè)試框架實(shí)現(xiàn)
- Python自動(dòng)化辦公之Word轉(zhuǎn)PDF的實(shí)現(xiàn)
- Python實(shí)現(xiàn)Harbor私有鏡像倉(cāng)庫(kù)垃圾自動(dòng)化清理詳情
- Python自動(dòng)化實(shí)戰(zhàn)之接口請(qǐng)求的實(shí)現(xiàn)
- Python實(shí)現(xiàn)自動(dòng)化處理每月考勤缺卡數(shù)據(jù)
相關(guān)文章
Python中遍歷字典過程中更改元素導(dǎo)致異常的解決方法
這篇文章主要介紹了Python中遍歷字典過程中更改元素導(dǎo)致錯(cuò)誤的解決方法,針對(duì)增刪元素后出現(xiàn)dictionary changed size during iteration的異常解決做出討論和解決,需要的朋友可以參考下2016-05-05
10 行Python 代碼實(shí)現(xiàn) AI 目標(biāo)檢測(cè)技術(shù)【推薦】
這篇文章主要介紹了10 行Python 代碼,實(shí)現(xiàn) AI 目標(biāo)檢測(cè)技術(shù),看完了代碼,我們?cè)谝黄鹆牧哪繕?biāo)檢測(cè)背后的技術(shù)背景,并解讀這10行Python代碼的由來和實(shí)現(xiàn)原理。感興趣的朋友跟隨小編一起看看吧2019-06-06
教你如何使用Python Tkinter庫(kù)制作記事本
讓我們看看如何使用 Tkinter 在 Python 中創(chuàng)建一個(gè)簡(jiǎn)單的記事本.這個(gè)記事本 GUI 將包含各種菜單,如文件和編輯,使用這些菜單可以完成保存文件、打開文件、編輯、剪切和粘貼等所有功能,需要的朋友可以參考下2021-06-06
Python實(shí)現(xiàn)序列化及csv文件讀取
這篇文章主要介紹了Python實(shí)現(xiàn)序列化及csv文件讀取,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
淺談Python數(shù)學(xué)建模之?dāng)?shù)據(jù)導(dǎo)入
數(shù)據(jù)導(dǎo)入是所有數(shù)模編程的第一步,比你想象的更重要。Python 語(yǔ)言中數(shù)據(jù)導(dǎo)入的方法很多。對(duì)于數(shù)學(xué)建模問題編程來說,選擇什么方法最好呢?答案是:沒有最好的,只有最合適的。對(duì)于不同的問題,不同的算法,以及所調(diào)用工具包的不同實(shí)現(xiàn)方法,對(duì)于數(shù)據(jù)就會(huì)有不同的要求2021-06-06

