9
9
"""
10
10
import logging
11
11
12
- import sh
12
+ import sarge
13
+ import structlog
13
14
14
- log = logging .getLogger ()
15
+ #log = logging.getLogger()
16
+ log = structlog .get_logger ()
15
17
16
18
def nslookup (domain ):
17
19
if not domain .endswith ('.' ):
18
20
domain = domain + '.'
19
- output = sh .nslookup (domain )
21
+ cmd = sarge .shell_format ('nslookup {0}' , domain )
22
+ log .info ('cmd' , cmd = cmd )
23
+ output = sarge .capture_both (cmd )
20
24
return output
21
25
22
26
def whois (domain ):
23
- output = sh .whois (domain )
27
+ cmd = sarge .shell_format ('whois {0}' , domain )
28
+ log .info ('cmd' , cmd = cmd )
29
+ output = sarge .capture_both (cmd )
24
30
return output
25
31
26
32
27
33
def dig_all (domain ):
28
- output = sh .dig (domain , "+cmd" , "+nocomments" , "+question" , "+noidentify" ,"+nostats" )
34
+ cmd = sarge .shell_format (
35
+ "dig {0} +cmd +nocomments +question +noidentify +nostats" ,
36
+ domain )
37
+ log .info ('cmd' , cmd = cmd )
38
+ output = sarge .capture_both (cmd )
29
39
return output
30
40
31
41
32
42
def dig_ns (domain ):
33
- output = sh .dig (domain , "ns" , "+cmd" , "+nocomments" , "+question" , "+noidentify" ,"+nostats" )
43
+ cmd = sarge .shell_format (
44
+ "dig {0} ns +cmd +nocomments +question +noidentify +nostats" ,
45
+ domain )
46
+ log .info ('cmd' , cmd = cmd )
47
+ output = sarge .capture_both (cmd )
34
48
return output
35
49
36
50
37
51
def dig_txt (domain ):
38
- output = sh .dig (domain , "txt" , "+cmd" , "+nocomments" , "+question" , "+noidentify" ,"+nostats" )
52
+ cmd = sarge .shell_format (
53
+ "dig {0} txt +cmd +nocomments +question +noidentify +nostats" ,
54
+ domain )
55
+ log .info ('cmd' , cmd = cmd )
56
+ output = sarge .capture_both (cmd )
39
57
return output
40
58
41
59
42
60
def dig_spf (domain ):
43
61
"""
44
62
https://en.wikipedia.org/wiki/Sender_Policy_Framework
45
63
"""
46
- output = sh .dig (domain , "spf" , "+cmd" , "+nocomments" , "+question" , "+noidentify" ,"+nostats" )
64
+ cmd = sarge .shell_format (
65
+ "dig {0} spf +cmd +nocomments +question +noidentify +nostats" ,
66
+ domain )
67
+ log .info ('cmd' , cmd = cmd )
68
+ output = sarge .capture_both (cmd )
47
69
return output
48
70
49
71
50
72
def dig_mx (domain ):
51
73
"""
52
74
https://en.wikipedia.org/wiki/MX_record
53
75
"""
54
- mx_output = sh .dig (domain , "mx" , "+cmd" , "+nocomments" , "+question" , "+noidentify" ,"+nostats" )
55
- return mx_output
76
+ cmd = sarge .shell_format (
77
+ "dig {0} mx +cmd +nocomments +question +noidentify +nostats" ,
78
+ domain )
79
+ log .info ('cmd' , cmd = cmd )
80
+ output = sarge .capture_both (cmd )
81
+ return output
56
82
57
83
58
84
def dig_dnskey (zone ):
59
- output = sh .dig ('+dnssec' , zone , 'dnskey' , "+cmd" , "+nocomments" , "+question" , "+noidentify" ,"+nostats" )
85
+ cmd = sarge .shell_format (
86
+ "dig {0} +dnssec dnskey +cmd +nocomments +question +noidentify +nostats" ,
87
+ zone )
88
+ log .info ('cmd' , cmd = cmd )
89
+ output = sarge .capture_both (cmd )
60
90
return output
61
91
62
92
63
93
def check_google_mx (domain ):
64
94
"""
65
95
https://support.google.com/a/topic/2716885?hl=en&ref_topic=2426592
66
96
"""
67
- output = sh .dig (domain , "mx" , "+short" ).rstrip ()
97
+ cmd = sarge .shell_format ("dig {0} mx +short" , domain )
98
+ log .info ('cmd' , cmd = cmd )
99
+ output = sarge .capture_both (cmd ).stdout .text .rstrip ()
68
100
log .debug (output )
69
101
result = None
70
102
check_domain = "aspmx.l.google.com."
@@ -85,7 +117,10 @@ def check_google_spf(domain):
85
117
"""
86
118
https://support.google.com/a/answer/178723?hl=en
87
119
"""
88
- output = sh .dig (domain , "txt" , "+short" ).rstrip ()
120
+ cmd = sarge .shell_format ("dig {0} txt +short" , domain )
121
+ log .info ('cmd' , cmd = cmd )
122
+ proc = sarge .capture_both (cmd )
123
+ output = proc .stdout .text .rstrip ()
89
124
log .debug (output )
90
125
expected = u"v=spf1 include:_spf.google.com ~all"
91
126
if output == expected :
@@ -100,23 +135,57 @@ def domain_tools(domain):
100
135
"""
101
136
mainfunc
102
137
"""
103
- print ("## whois: %r" % domain )
104
- print (whois (domain ))
105
-
106
- print ("## dig: %r" % domain )
107
- print (dig_all (domain ))
108
-
109
- print (dig_ns (domain ))
110
-
111
- print (dig_mx (domain ))
112
-
113
- print (dig_txt (domain ))
114
-
115
- print (dig_spf (domain ))
116
-
117
- print (dig_dnskey (domain .split ("." )[- 1 ])) # TODO: actual zone
118
-
119
- return 0
138
+ returncode = 0
139
+ proc = whois (domain )
140
+ returncode += proc .returncode
141
+ print (proc .stdout .text )
142
+ print ('-' )
143
+ stderr = proc .stderr .text ; stderr and print (stderr )
144
+ print ('--' )
145
+
146
+ proc = dig_all (domain )
147
+ returncode += proc .returncode
148
+ print (proc .stdout .text )
149
+ print ('-' )
150
+ stderr = proc .stderr .text ; stderr and print (stderr )
151
+ print ('--' )
152
+
153
+ proc = dig_ns (domain )
154
+ returncode += proc .returncode
155
+ print (proc .stdout .text )
156
+ print ('-' )
157
+ stderr = proc .stderr .text ; stderr and print (stderr )
158
+ print ('--' )
159
+
160
+ proc = dig_mx (domain )
161
+ returncode += proc .returncode
162
+ print (proc .stdout .text )
163
+ print ('-' )
164
+ stderr = proc .stderr .text ; stderr and print (stderr )
165
+ print ('--' )
166
+
167
+ proc = dig_txt (domain )
168
+ returncode += proc .returncode
169
+ print (proc .stdout .text )
170
+ print ('-' )
171
+ stderr = proc .stderr .text ; stderr and print (stderr )
172
+ print ('--' )
173
+
174
+ proc = dig_spf (domain )
175
+ returncode += proc .returncode
176
+ print (proc .stdout .text )
177
+ print ('-' )
178
+ stderr = proc .stderr .text ; stderr and print (stderr )
179
+ print ('--' )
180
+
181
+ proc = dig_dnskey (domain .split ("." )[- 1 ]) # TODO: actual zone
182
+ returncode += proc .returncode
183
+ print (proc .stdout .text )
184
+ print ('-' )
185
+ stderr = proc .stderr .text ; stderr and print (stderr )
186
+ print ('--' )
187
+
188
+ return returncode
120
189
121
190
122
191
def google_domain_tools (domain ):
@@ -175,14 +244,17 @@ def main(*args):
175
244
import unittest
176
245
sys .exit (unittest .main ())
177
246
178
- retcode = 0
179
- retcode = domain_tools (domain )
247
+ returncode = 0
248
+ returncode += domain_tools (domain )
249
+ print ("domain_tools: %d" % returncode )
180
250
181
251
if opts .google_domain_tools :
182
252
print ("## google_domain_tools: %r" % domain )
183
- retcode = retcode + google_domain_tools (domain )
253
+ returncode += google_domain_tools (domain )
254
+
255
+ print ("google_domain_tools: %d" % returncode )
184
256
185
- return retcode
257
+ return returncode
186
258
187
259
188
260
if __name__ == "__main__" :
0 commit comments