Skip to content

Commit c39355e

Browse files
committed
ENH,BLD: wrdrd.tools.domain: sarge, structlog (#7)
1 parent 524be50 commit c39355e

File tree

4 files changed

+114
-37
lines changed

4 files changed

+114
-37
lines changed

requirements-dev.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
-e git+https://github.com/amoffat/sh#egg=sh
1+
-e hg+https://bitbucket.org/vinay.sajip/sarge#egg=sarge
2+
-e git+https://github.com/hynek/structlog#egg=structlog

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
sh
1+
sarge
2+
structlog
23
requests
34
beautifulsoup4
45
URLObject

setup.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
readme = open('README.rst').read()
1818
history = open('HISTORY.rst').read().replace('.. :changelog:', '')
1919

20-
requires = ['sh']
20+
requires = [
21+
'sarge',
22+
'structlog',
23+
]
2124

2225
setup(
2326
name='wrdrd',

wrdrd/tools/domain.py

+106-34
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,94 @@
99
"""
1010
import logging
1111

12-
import sh
12+
import sarge
13+
import structlog
1314

14-
log = logging.getLogger()
15+
#log = logging.getLogger()
16+
log = structlog.get_logger()
1517

1618
def nslookup(domain):
1719
if not domain.endswith('.'):
1820
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)
2024
return output
2125

2226
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)
2430
return output
2531

2632

2733
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)
2939
return output
3040

3141

3242
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)
3448
return output
3549

3650

3751
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)
3957
return output
4058

4159

4260
def dig_spf(domain):
4361
"""
4462
https://en.wikipedia.org/wiki/Sender_Policy_Framework
4563
"""
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)
4769
return output
4870

4971

5072
def dig_mx(domain):
5173
"""
5274
https://en.wikipedia.org/wiki/MX_record
5375
"""
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
5682

5783

5884
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)
6090
return output
6191

6292

6393
def check_google_mx(domain):
6494
"""
6595
https://support.google.com/a/topic/2716885?hl=en&ref_topic=2426592
6696
"""
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()
68100
log.debug(output)
69101
result = None
70102
check_domain = "aspmx.l.google.com."
@@ -85,7 +117,10 @@ def check_google_spf(domain):
85117
"""
86118
https://support.google.com/a/answer/178723?hl=en
87119
"""
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()
89124
log.debug(output)
90125
expected = u"v=spf1 include:_spf.google.com ~all"
91126
if output == expected:
@@ -100,23 +135,57 @@ def domain_tools(domain):
100135
"""
101136
mainfunc
102137
"""
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
120189

121190

122191
def google_domain_tools(domain):
@@ -175,14 +244,17 @@ def main(*args):
175244
import unittest
176245
sys.exit(unittest.main())
177246

178-
retcode = 0
179-
retcode = domain_tools(domain)
247+
returncode = 0
248+
returncode += domain_tools(domain)
249+
print("domain_tools: %d" % returncode)
180250

181251
if opts.google_domain_tools:
182252
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)
184256

185-
return retcode
257+
return returncode
186258

187259

188260
if __name__ == "__main__":

0 commit comments

Comments
 (0)