forked from jforman/python-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintzone.py
More file actions
executable file
·47 lines (38 loc) · 1.48 KB
/
printzone.py
File metadata and controls
executable file
·47 lines (38 loc) · 1.48 KB
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
#!/usr/bin/env python
from optparse import OptionParser
import dns.query
import dns.zone
import socket
import sys
parser = OptionParser()
parser.add_option("--server", dest="dns_server",
help="DNS server to query.",
default = False,
type="string")
parser.add_option("--zone", dest="dns_zone",
help="Zone to print.",
default = False,
type="string")
(options, args) = parser.parse_args()
if ((options.dns_server is False) or (options.dns_zone is False)):
print "You forgot to specify both a server and a DNS zone. Exiting.\n"
sys.exit(1)
print "Server: %s Zone: %s" % (options.dns_server, options.dns_zone)
try:
try:
z = dns.zone.from_xfr(dns.query.xfr(options.dns_server, options.dns_zone))
except socket.gaierror, e:
print "Problems querying DNS server %s: %s\n" % (options.dns_server, e)
sys.exit(1)
names = z.nodes.keys()
names.sort() # Sort the array alphabetically
zone_xfr_array = []
for n in names:
current_record = z[n].to_text(n)
for split_record in current_record.split("\n"): # Split the records on the newline
zone_xfr_array.append([split_record]) # Add each record to our array
except dns.exception.FormError:
print "The transfer encountered a problem. Check your zone records.\n"
sys.exit(1)
for current_record in zone_xfr_array: # Iterate and print our DNS records array
print current_record