網站首頁 編程語言 正文
腳本架構:
- domain_test.py:批量解析運行主程序
- DomainResult.txt:域名解析結果文件
- domains.txt:解析的域名文件
實現代碼如下:
# 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)
#解析完成后,生成結果文件
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='')
演示結果:
原文鏈接:https://blog.51cto.com/pikaqiu/5527407
相關推薦
- 2023-05-14 Python中數字(Number)數據類型常用操作_python
- 2022-02-15 小程序搜索框歷史記錄,去除重復搜索內容,限制顯示條數
- 2022-06-07 FreeRTOS實時操作系統移植操作示例指南_操作系統
- 2023-04-20 npm ERR! 400 Bad Request - PUT xxx - Cannot publis
- 2023-03-01 Python第三方庫undetected_chromedriver的使用_python
- 2023-01-23 Python+Qt身體特征識別人數統計源碼窗體程序(使用步驟)_python
- 2022-04-03 Python中bytes字節串和string字符串之間的轉換方法_python
- 2022-10-16 Python實現自定義Jupyter魔法命令_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支