forked from mrlesmithjr/python-powerdns-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdns.py
executable file
·515 lines (451 loc) · 20.1 KB
/
pdns.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python
"""
pdns.py: Manage PowerDNS Zones/Records
"""
import argparse
import csv
import json
import requests
__author__ = "Larry Smith Jr."
__maintainer__ = "Larry Smith Jr."
__status__ = "Development"
# http://everythingshouldbevirtual.com
# @mrlesmithjr
EXAMPLES = """
Create a new Master Zone with info below:
-----------------------------------------
Zone: dev.vagrant.local
ZoneType: MASTER
Master: 172.28.128.3
Nameservers:
172.28.128.3
172.28.128.4
172.28.128.5
./pdns.py add_zones --apihost 172.28.128.3 --zone dev.vagrant.local --zoneType MASTER --nameservers 172.28.128.3,172.28.128.4,172.28.128.5
Create the Slave Zones with the info below:
-------------------------------------------
Zone: dev.vagrant.local
ZoneType: SLAVE
Master: 172.28.128.3
Slaves:
172.28.128.4
172.28.128.5
./pdns.py add_zones --apihost 172.28.128.4 --zone dev.vagrant.local --zoneType SLAVE --masters 172.28.128.3
Create Master Zones using a CSV file:
-------------------------------------
Create a master_zones.csv similar to below:
zone,zoneType,masters,nameservers
128.28.172.in-addr.arpa,MASTER,,"172.28.128.3,172.28.128.4,172.28.128.5"
dev.vagrant.local,MASTER,,"172.28.128.3,172.28.128.4,172.28.128.5"
prod.vagrant.local,MASTER,,"172.28.128.3,172.28.128.4,172.28.128.5"
test.vagrant.local,MASTER,,"172.28.128.3,172.28.128.4,172.28.128.5"
vagrant.local,MASTER,,"172.28.128.3,172.28.128.4,172.28.128.5"
The first row is the header...
Now read the csv file using CLI argument:
./pdns.py add_zones --apihost 172.28.128.3 --readcsv master_zones.csv
Create records with info below:
-------------------------------
Zone: dev.vagrant.local
name: test01.dev.vagrant.local
recordType: A
content: 172.28.128.161
name: development.dev.vagrant.local
recordType: CNAME
content: test01.dev.vagrant.local
./pdns.py add_records --zone dev.vagrant.local --name test01 --content 172.28.128.161 --recordType A
./pdns.py add_records --zone dev.vagrant.local --name development --content test01.dev.vagrant.local --recordType CNAME
Create records using a csv file:
---------------------------------------
Create a add_records.csv file similar to below:
name,zone,record_type,content,disabled,ttl,set_ptr,priority
development,test.vagrant.local,A,172.28.128.3,FALSE,3600,TRUE,0
node0,test.vagrant.local,A,172.28.128.4,FALSE,3600,TRUE,0
node1,test.vagrant.local,A,172.28.128.5,FALSE,3600,TRUE,0
node100,test.vagrant.local,A,172.28.128.100,FALSE,3600,TRUE,0
node101,test.vagrant.local,A,172.28.128.101,FALSE,3600,TRUE,0
node102,test.vagrant.local,A,172.28.128.102,FALSE,3600,TRUE,0
node2,dev.vagrant.local,A,172.28.128.201,FALSE,3600,TRUE,0
node201,dev.vagrant.local,A,172.28.128.202,FALSE,3600,TRUE,0
node202,dev.vagrant.local,A,172.28.128.203,FALSE,3600,TRUE,0
node203,dev.vagrant.local,CNAME,node201.dev.vagrant.local,FALSE,3600,TRUE,0
smtp,vagrant.local,A,172.28.128.20,FALSE,3600,TRUE,0
mail,vagrant.local,CNAME,smtp.vagrant.local,FALSE,3600,TRUE,0
The first row is the header...
Now read the csv file using CLI argument:
./pdns.py add_records --apihost 172.28.128.3 --readcsv add_records.csv
Delete records with info below:
-------------------------------
Zone: vagrant.local
name: smtp.vagrant.local
recordType: A
./pdns.py delete_records --apihost 172.28.128.3 --name smtp --zone vagrant.local --recordType A
Delete records reading from a csv file:
---------------------------------------
Create a delete_records.csv similar to below:
name,zone,record_type
node100,test.vagrant.local,A
node101,test.vagrant.local,A
node202,dev.vagrant.local,A
node203,dev.vagrant.local,CNAME
The first row is the header...
Now read the csv file using CLI argument:
./pdns.py delete_records --apihost 172.28.128.3 --readcsv delete_records.csv
Query PDNS config
-----------------
./pdns.py query_config --apihost 172.28.128.3
Query zones
-----------
./pdns.py query_zones --apihost 172.28.128.3
Api version
-----------
./pdns.py query_config --apihost 172.28.128.3 --apiversion old
Default is PDNS v4.0.x version v1 api /api/v1
For PDNS v3.x use --apiversion old
"""
class PDNSControl(object):
"""
Main execution
"""
def __init__(self):
self.read_cli_args()
self.setup_api_call()
self.decide_action()
def add_records(self):
"""
Add new DNS records
Create new DNS records of different types
"""
if self.args.readcsv is None:
payload = {
"rrsets": [
{
"name": self.args.name + '.' + self.args.zone,
"type": self.args.recordType,
"changetype": "REPLACE",
"records": [
{
"content": self.args.content,
"disabled": self.args.disabled,
"name": self.args.name + '.' + self.args.zone,
"ttl": self.args.ttl,
"set-ptr": self.args.setPTR,
"type": self.args.recordType,
"priority": self.args.priority
}
]
}
]
}
zone_check = requests.get(self.uri, headers=self.headers)
if zone_check.status_code == 200:
dummy_r = requests.patch(self.uri, data=json.dumps(payload), headers=self.headers)
print ("DNS Record '%s' Successfully Added/Updated"
% (self.args.name + '.' + self.args.zone))
else:
print "DNS Zone '%s' Does Not Exist..." % self.args.zone
elif self.args.readcsv is not None:
try:
f = open(self.args.readcsv)
csv_f = csv.reader(f)
next(csv_f, None) #skip headers
for row in csv_f:
uri = ("http://%s:%s%s/servers/localhost/zones/%s"
%(self.args.apihost, self.args.apiport, self.args.apiversion, row[1]))
if row[4].lower() == "false":
disabled = False
elif row[4].lower() == "true":
disabled = True
if row[6].lower() == "false":
set_ptr = False
if row[6].lower() == "true":
set_ptr = True
payload = {
"rrsets": [
{
"name": row[0] + '.' + row[1],
"type": row[2],
"changetype": "REPLACE",
"records": [
{
"content": row[3],
"disabled": disabled,
"name": row[0] + '.' + row[1],
"ttl": row[5],
"set-ptr": set_ptr,
"type": row[2],
"priority": row[7]
}
]
}
]
}
zone_check = requests.get(uri, headers=self.headers)
if zone_check.status_code == 200:
dummy_r = (requests.patch(uri, data=json.dumps(payload),
headers=self.headers))
print ("DNS Record '%s' Successfully Added/Updated"
% (row[0] + '.' + row[1]))
else:
print "DNS Zone '%s' Does Not Exist...Skipping" % row[1]
finally:
f.close()
def add_zones(self):
"""
Add new DNS zones
Create Master, Native or Slave zones
"""
if self.args.readcsv is None:
masters = []
nameservers = []
if self.args.masters:
for master in self.args.masters.split(','):
masters.append(master)
if self.args.nameservers:
for nameserver in self.args.nameservers.split(','):
nameservers.append(nameserver)
if self.args.zoneType == "MASTER":
payload = {
"name": self.args.zone + '.',
"kind": self.args.zoneType,
"masters": [],
"soa_edit_api": "INCEPTION-INCREMENT",
"nameservers": nameservers
}
elif self.args.zoneType == "NATIVE":
payload = {
"name": self.args.zone + '.',
"kind": self.args.zoneType,
"masters": [],
"nameservers": nameservers
}
else:
payload = {
"name": self.args.zone + '.',
"kind": self.args.zoneType,
"masters": masters,
"nameservers": []
}
zone_check_uri = ("http://%s:%s%s/servers/localhost/zones/%s"
%(self.args.apihost, self.args.apiport, self.args.apiversion, self.args.zone))
zone_check = requests.get(zone_check_uri, headers=self.headers)
if zone_check.status_code == 200:
print "DNS Zone '%s' Already Exists..." % self.args.zone
else:
dummy_r = requests.post(self.uri, data=json.dumps(payload), headers=self.headers)
print "DNS Zone '%s' Successfully Added..." % self.args.zone
elif self.args.readcsv is not None:
try:
f = open(self.args.readcsv)
csv_f = csv.reader(f)
next(csv_f, None) #skip headers
for row in csv_f:
masters = []
nameservers = []
zone_check_uri = ("http://%s:%s%s/servers/localhost/zones/%s"
%(self.args.apihost, self.args.apiport, self.args.apiversion, row[0]))
zone_check = requests.get(zone_check_uri, headers=self.headers)
if zone_check.status_code == 200:
print "DNS Zone '%s' Already Exists...Skipping" % row[0]
else:
if row[2]:
for master in row[2].split(','):
masters.append(master)
if row[3]:
for nameserver in row[3].split(','):
nameservers.append(nameserver)
if row[1].upper() == "MASTER":
payload = {
"name": row[0] + '.',
"kind": row[1],
"masters": [],
"soa_edit_api": "INCEPTION-INCREMENT",
"nameservers": nameservers
}
elif row[1].upper() == "NATIVE":
payload = {
"name": row[0] + '.',
"kind": row[1],
"masters": [],
"nameservers": nameservers
}
else:
payload = {
"name": row[0] + '.',
"kind": row[1],
"masters": masters,
"nameservers": []
}
dummy_r = (requests.post(self.uri,
data=json.dumps(payload), headers=self.headers))
print "DNS Zone '%s' Successfully Added..." % row[0]
finally:
f.close()
def decide_action(self):
"""
Determine action
Based on action passed determine which action to take
"""
if self.args.action == "add_records":
self.add_records()
elif self.args.action == "add_zones":
self.add_zones()
elif self.args.action == "delete_records":
self.delete_records()
elif self.args.action == "delete_zones":
self.delete_zones()
elif self.args.action == "query_config":
self.query_config()
elif self.args.action == "query_stats":
self.query_stats()
elif self.args.action == "query_zones":
self.query_zones()
def delete_records(self):
"""
Delete DNS records
Delete DNS records of different types
"""
if self.args.readcsv is None:
payload = {
"rrsets": [
{
"name": self.args.name + '.' + self.args.zone + '.',
"type": self.args.recordType,
"changetype": "DELETE",
}
]
}
zone_check = requests.get(self.uri, headers=self.headers)
if zone_check.status_code == 200:
dummy_r = requests.patch(self.uri, data=json.dumps(payload), headers=self.headers)
print ("DNS Record '%s' Successfully Deleted"
% (self.args.name + '.' + self.args.zone))
else:
print "DNS Zone '%s' Does Not Exist..." % self.args.zone
elif self.args.readcsv is not None:
try:
f = open(self.args.readcsv)
csv_f = csv.reader(f)
next(csv_f, None) #skip headers
for row in csv_f:
uri = ("http://%s:%s%s/servers/localhost/zones/%s"
%(self.args.apihost, self.args.apiport, self.args.apiversion, row[1]))
payload = {
"rrsets": [
{
"name": row[0] + '.' + row[1] '.',
"type": row[2],
"changetype": "DELETE",
}
]
}
zone_check = requests.get(uri, headers=self.headers)
if zone_check.status_code == 200:
dummy_r = (requests.patch(uri, data=json.dumps(payload),
headers=self.headers))
print ("DNS Record '%s' Successfully Deleted"
% (row[0] + '.' + row[1]))
else:
print "DNS Zone '%s' Does Not Exist...Skipping" % row[1]
finally:
f.close()
def delete_zones(self):
"""
Delete DNS Zones
"""
payload = {
"name": self.args.zone + '.'
}
zone_check = requests.get(self.uri, headers=self.headers)
if zone_check.status_code == 200:
dummy_r = requests.delete(self.uri, data=json.dumps(payload), headers=self.headers)
print "DNS Zone '%s' Successfully Deleted..." % self.args.zone
else:
print "DNS Zone '%s' Does Not Exist..." % self.args.zone
def query_config(self):
"""
Query PDNS Config
"""
r = requests.get(self.uri, headers=self.headers)
python_data = json.loads(r.text)
print json.dumps(python_data, indent=4)
def query_stats(self):
"""
Query DNS Stats
"""
r = requests.get(self.uri, headers=self.headers)
python_data = json.loads(r.text)
print json.dumps(python_data, indent=4)
def query_zones(self):
"""
Query DNS Zones
Query existing DNS Zones
"""
r = requests.get(self.uri, headers=self.headers)
if r.status_code == 200:
python_data = json.loads(r.text)
print json.dumps(python_data, indent=4)
else:
print "DNS Zone '%s' Does Not Exist..." % self.args.zone
def read_cli_args(self):
"""
Read variables from CLI
Read CLI variables passed on CLI
"""
parser = argparse.ArgumentParser(description='PDNS Controls...')
parser.add_argument('action', help='Define action to take',
choices=['add_records', 'add_zones', 'delete_records',
'delete_zones', 'query_config', 'query_stats', 'query_zones'])
parser.add_argument('--apikey', help='PDNS API Key', default='changeme')
parser.add_argument('--apihost', help='PDNS API Host', default='127.0.0.1')
parser.add_argument('--apiversion', help='PDNS API version', choices=['old', '/api/v1'], default='/api/v1')
parser.add_argument('--apiport', help='PDNS API Port', default='8081')
parser.add_argument('--content', help='DNS Record content')
parser.add_argument('--disabled', help='Define if Record is disabled',
choices=['True', 'False'], default=False)
parser.add_argument('--masters', help='DNS zone masters')
parser.add_argument('--name', help='DNS record name')
parser.add_argument('--nameservers', help='DNS nameservers for zone')
parser.add_argument('--priority', help='Define priority', default=0)
parser.add_argument('--readcsv', help='Read input from CSV')
parser.add_argument('--recordType', help='DNS record type',
choices=['A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT'])
parser.add_argument('--setPTR', help='Define if PTR record is created',
choices=['True', 'False'], default=True)
parser.add_argument('--ttl', help='Define TTL', default=3600)
parser.add_argument('--zone', help='DNS zone')
parser.add_argument('--zoneType', help='DNS Zone Type',
choices=['MASTER', 'NATIVE', 'SLAVE'])
self.args = parser.parse_args()
if self.args.apiversion == "old":
self.args.apiversion = ""
if self.args.action == "add_zones" and (self.args.zoneType == "MASTER" and
self.args.nameservers is None):
parser.error("--nameservers is required to create MASTER zone")
if self.args.action == "add_zones" and (self.args.zoneType == "SLAVE" and
self.args.masters is None):
parser.error("--masters is required to create SLAVE zone")
def setup_api_call(self):
"""
Setup API Call
Based on action setup the correct API call to make
"""
self.headers = {'X-API-Key': self.args.apikey}
if (self.args.action == "add_zones" or (self.args.action == "query_zones" and
self.args.zone is None)):
self.uri = ("http://%s:%s%s/servers/localhost/zones"
%(self.args.apihost, self.args.apiport, self.args.apiversion))
elif ((self.args.action == "add_records" and self.args.readcsv is None)
or (self.args.action == "delete_records" and self.args.readcsv is None)
or self.args.action == "delete_zones" or
(self.args.action == "query_zones" and self.args.zone is not None)):
self.uri = ("http://%s:%s%s/servers/localhost/zones/%s"
%(self.args.apihost, self.args.apiport, self.args.apiversion, self.args.zone))
elif self.args.action == "query_config":
self.uri = ("http://%s:%s%s/servers/localhost/config"
%(self.args.apihost, self.args.apiport, self.args.apiversion))
elif self.args.action == "query_stats":
self.uri = ("http://%s:%s%s/servers/localhost/statistics"
%(self.args.apihost, self.args.apiport, self.args.apiversion))
if __name__ == '__main__':
PDNSControl()