脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现自动化域名批量解析分享

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

2022-08-01 14:28爱搞网络的皮卡丘​​​ Python

这篇文章主要介绍了Python实现自动化域名批量解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下

脚本架构:

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

  • domain_test.py:批量解析运行主程序
  • DomainResult.txt:域名解析结果文件
  • domains.txt:解析的域名文件

实现代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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实现自动化域名批量解析分享

到此这篇关于Python实现自动化域名批量解析的文章就介绍到这了,更多相关Python自动化域名解析内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.51cto.com/pikaqiu/5527407

延伸 · 阅读

精彩推荐
  • PythonPython+微信接口实现运维报警

    Python+微信接口实现运维报警

    这篇文章主要介绍了Python+微信接口实现运维报警的相关资料,需要的朋友可以参考下...

    mindg4132020-09-05
  • Pythonpytorch forward两个参数实例

    pytorch forward两个参数实例

    今天小编就为大家分享一篇pytorch forward两个参数实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    weixin_4195027610272020-04-18
  • PythonPython中新式类与经典类的区别详析

    Python中新式类与经典类的区别详析

    这篇文章主要给大家介绍了关于Python中新式类与经典类的区别,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需...

    BlackMatrix11432021-08-07
  • PythonPython实现的根据IP地址计算子网掩码位数功能示例

    Python实现的根据IP地址计算子网掩码位数功能示例

    这篇文章主要介绍了Python实现的根据IP地址计算子网掩码位数功能,涉及Python数值运算相关操作技巧,需要的朋友可以参考下...

    petrel20159322021-02-23
  • PythonPython使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例

    Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能

    这篇文章主要介绍了Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能,涉及Python基于re模块的正则匹配及selenium模块的页面抓取等相关操作技巧...

    wanlifeipeng11042021-03-18
  • PythonPython用户自定义异常的实现

    Python用户自定义异常的实现

    这篇文章主要介绍了Python用户自定义异常的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    xiaobai小白12422021-08-18
  • Python如何利用Python爬取抖音APP数据

    如何利用Python爬取抖音APP数据

    相信大家都有过手滑的时刻,在这个信息流的世界,有时候错过了,想再找到某条信息简直就是大海捞针,自己手动去找肯定是很浪费时间的。今天,我们...

    编程小清9362021-04-26
  • Python解决PyCharm中光标变粗的问题

    解决PyCharm中光标变粗的问题

    下面小编就为大家带来一篇解决PyCharm中光标变粗的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    脚本之家8822020-11-30