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)文章
Flask框架踩坑之a(chǎn)jax跨域請(qǐng)求實(shí)現(xiàn)
這篇文章主要介紹了Flask框架踩坑之a(chǎn)jax跨域請(qǐng)求實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
python安裝庫(kù)的最詳細(xì)方法(以安裝pygame庫(kù)為例)
在學(xué)習(xí)了一個(gè)學(xué)期的python之后,我決定對(duì)pygame下手了,下面這篇文章主要給大家介紹了關(guān)于python安裝庫(kù)的最詳細(xì)方法,本文主要以安裝pygame庫(kù)為例,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
python使用matplotlib畫出的圖怎樣放到word中
這篇文章主要介紹了python使用matplotlib畫出的圖怎樣放到word中問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python3 關(guān)于pycharm自動(dòng)導(dǎo)入包快捷設(shè)置的方法
今天小編就為大家分享一篇Python3 關(guān)于pycharm自動(dòng)導(dǎo)入包快捷設(shè)置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python中列表(List) 的三種遍歷(序號(hào)和值)方法小結(jié)
這篇文章主要介紹了Python中列表(List) 的三種遍歷(序號(hào)和值)方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
詳解如何用Python寫個(gè)聽(tīng)小說(shuō)的爬蟲(chóng)
在路上經(jīng)常發(fā)現(xiàn)好多人都喜歡用耳機(jī)聽(tīng)小說(shuō),同事居然可以一整天的帶著一只耳機(jī)聽(tīng)小說(shuō)。本文就用Python爬蟲(chóng)實(shí)現(xiàn)下載聽(tīng)小說(shuō)tingchina.com的音頻,需要的可以參考一下2022-02-02
Python實(shí)現(xiàn)windows下模擬按鍵和鼠標(biāo)點(diǎn)擊的方法
這篇文章主要介紹了Python實(shí)現(xiàn)windows下模擬按鍵和鼠標(biāo)點(diǎn)擊的方法,涉及Python模擬實(shí)現(xiàn)鼠標(biāo)及鍵盤事件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

