-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyNslookup.py
150 lines (134 loc) · 4.54 KB
/
MyNslookup.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import dns.resolver
import sys
"""
Nslookup程序(获取A记录,CNAME,MX,TXT,DNS,MX等信息)
完成但是有些记录不太行
"""
class pynslookup:
def __init__(self, domain='', type=''):
self.domain = domain
self.type = type
def bytetostring(self, bv):
sv = bv.decode()
return sv
def getSPFKey(self, domain):
spf = 'spf' + "." + domain
return spf
def getSPFValue(self, domain):
try:
answersSPF = dns.resolver.resolve(self.getSPFKey(domain), 'TXT')
for rdata in answersSPF:
for txt_string in rdata.strings:
txt_string = self.bytetostring(txt_string)
return txt_string
except dns.resolver.NoAnswer:
# print('NO TXT Record')
return 'None'
def getTvalue(self, domain):
try:
answersTXT = dns.resolver.resolve(domain, 'TXT')
for tdata in answersTXT:
for txt_string in tdata.strings:
txt_string = self.bytetostring(txt_string)
return txt_string
except dns.resolver.NoAnswer:
# print('NO TXT Record')
return 'None'
def getMXvalue(self, domain):
try:
resultMX = dns.resolver.resolve(domain, 'MX')
for exdata in resultMX:
res = exdata.to_text()
av = res.split(' ')
return av[1]
except dns.resolver.NoAnswer:
# print('NO MX Record')
return 'None'
def getCNAMEvalue(self, domain):
try:
result = dns.resolver.resolve(domain, 'CNAME')
# print('result ', result)
for ip in result:
return ip.to_text()
except dns.resolver.NoAnswer:
# print('NO CNAME Record')
return 'None'
def getAvalue(self, domain):
try:
resultA = dns.resolver.resolve(domain, 'A')
for ip in resultA:
return ip.to_text()
except dns.resolver.NoAnswer:
# print('NO A Record')
return 'None'
def result(self, domain, type=''):
nslookup_result = {}
if type == '':
nslookup_result['domain'] = domain
nslookup_result['A'] = self.getAvalue(domain)
nslookup_result['CNAME'] = self.getCNAMEvalue(domain)
nslookup_result['MX'] = self.getMXvalue(domain)
nslookup_result['TXT'] = self.getTvalue(domain)
# print(nslookup_result)
return nslookup_result
elif type == 'A':
nslookup_result['domain'] = domain
nslookup_result['A'] = self.getAvalue(domain)
# print(nslookup_result)
return nslookup_result
elif type == 'CNAME':
nslookup_result['domain'] = domain
nslookup_result['CNAME'] = self.getAvalue(domain)
# print(nslookup_result)
return nslookup_result
elif type == 'MX':
nslookup_result['domain'] = domain
nslookup_result['MX'] = self.getAvalue(domain)
# print(nslookup_result)
return nslookup_result
elif type == 'TXT':
nslookup_result['domain'] = domain
nslookup_result['TXT'] = self.getAvalue(domain)
# print(nslookup_result)
return nslookup_result
else:
print('type is error!')
exit(1)
if __name__ == '__main__':
title = '''
Nslookup by Python
Get the record, CNAME, MX, TXT, DNS, MX and other information
Usage:
python3 pynslookup.py www.baidu.com
'''
params = sys.argv
if sys.argv[1] == '-h':
print(title)
else:
domain = sys.argv[1]
result = pynslookup()
res = result.result(domain)
for k, v in res.items():
print(k, ': ', v)
# result.result('bilibili.com')
# for k,v in result:
# print(k,': ',v)
#
# re = getCNAMEvalue('www.baidu.com')
# print(re)
# re = getAvalue('www.baidu.com')
# print(re)
# file_r = "wang.txt"
# file_w = "res.json"
#
# with open(file_r, 'r') as file_object_r:
# lines = file_object_r.readlines()
# for line in lines:
# line = line.strip('\n')
# mx = getMXvalue(line)
# ip = getAvalue(getMXvalue(line))
# txt = getTvalue(line)
# spf = getSPFValue(line)
# res = mx + "#" + ip + "#" + txt + "#" + spf
# with open(file_w, 'a') as file_object_w:
# file_object_w.write(res + '\n')