Python实现自动化域名批量解析分享

作者:爱搞网络的皮卡丘??????? 时间:2023-01-27 00:04:36 

脚本架构:

Python实现自动化域名批量解析分享

  • 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='')

演示结果:

Python实现自动化域名批量解析分享

来源:https://blog.51cto.com/pikaqiu/5527407

标签:Python,实现,自动化,域名,批量,解析
0
投稿

猜你喜欢

  • Python实现简易超市管理系统

    2023-01-08 12:09:01
  • PDO::_construct讲解

    2023-06-06 03:22:31
  • snoopy PHP版的网络客户端提供本地下载

    2023-06-28 00:09:55
  • ThinkPHP3.1.3版本新特性概述

    2023-09-28 15:25:11
  • python小项目之五子棋游戏

    2022-07-12 06:24:23
  • Webpack4 使用Babel处理ES6语法的方法示例

    2023-08-30 08:12:37
  • 在Oracle中向视图中插入数据的方法

    2009-02-28 10:42:00
  • MySQL 密码设置

    2024-01-28 11:53:59
  • 将DataTable作为存储过程参数的用法实例详解

    2024-01-27 14:23:24
  • C#中通过使用Connection类来实现打开/关闭数据库的代码实例

    2024-01-21 22:36:55
  • python调用百度REST API实现语音识别

    2023-08-28 19:44:44
  • 利用脚本自动安装SQLServer的实现步骤分析

    2024-01-22 20:02:11
  • PHP错误Warning: Cannot modify header information - headers already sent by解决方法

    2023-11-15 11:53:16
  • Python闭包技巧介绍

    2022-05-30 19:17:04
  • python 如何上传包到pypi

    2023-12-22 13:31:41
  • 深入理解Python中变量赋值的问题

    2023-03-20 00:28:07
  • Python Sql数据库增删改查操作简单封装

    2024-01-22 14:47:37
  • C#动态创建Access数据库及密码的方法

    2024-01-17 21:37:21
  • 在vant中如何使用dialog弹窗

    2024-05-22 10:41:50
  • python基础之文件操作

    2022-12-23 01:15:53
  • asp之家 网络编程 m.aspxhome.com