# dns_resolv.py
import dns.resolver
while True:
domain = input('请输入要解析的域名(输入"q"退出):')
if domain == 'q':
break
print('*******主机记录解析*********')
try:
A = dns.resolver.resolve(domain, 'A') # 解析类型为A记录
for m in A.response.answer:
for n in m.items:
# 通过判断排除没有IP地址的CNAME对象
if n.rdtype == 1:
print('IP地址:',n.address)
except Exception as e:
print(e)
print('*******别名记录解析*********')
try:
CNAME = dns.resolver.resolve(domain,'CNAME') # 解析类型为CNAME记录
for m in CNAME.response.answer:
for n in m.items:
print('别名:',n.to_text())
except Exception as e:
print(e)
print('*******邮件服务器记录解析*********')
try:
MX = dns.resolver.resolve(domain, 'MX') # 解析类型为MX记录
for m in MX:
print('邮件服务器:', m.exchange, '优先级:', m.preference)
except Exception as e:
print(e)
print('*******名称服务器记录解析*********')
try:
NS = dns.resolver.resolve(domain, 'NS') # 解析类型为NS记录
for m in NS.response.answer:
for n in m.items:
print('名称服务器:',m.to_text())
except Exception as e:
print(e)
# ipy_solv.py
from IPy import IP
while True:
ip_inp = input('请输入IP地址或网络地址(输入"q"退出):')
if ip_inp == 'q':
break # 退出
try:
ips = IP(ip_inp)
except Exception as e:
print(e)
continue
if len(ips) > 1: # IP地址个数大于1
print('网络地址: %s' % ips.net())
print('网络掩码: %s' % ips.netmask())
print('网络前缀长度: %s' % ips.prefixlen())
print('广播地址: %s' % ips.broadcast())
print('反向解析地址: %s' % ips.reverseNames()[0])
print('子网IP地址数:%s' % len(ips))
else: # 单个IP地址的情形
print('反向解析地址: %s' % ips.reverseName())
print('整数形式: %s' % ips.int())
print('十六进制形式: %s' % ips.strHex())
print('二进制形式: %s' % ips.strBin())
print('IP地址类型: %s' % ips.iptype())