-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsd-catz-add.py
executable file
·61 lines (50 loc) · 1.7 KB
/
nsd-catz-add.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
#!/usr/bin/env python3
zonelistfile = '/var/db/nsd/zone.list'
import os
import sys
import uuid
from datetime import datetime
serial = int(datetime.now().strftime('%Y%m%d00'))
if len(sys.argv) != 3:
print("usage %s <catalog> <zone>" % sys.argv[0])
sys.exit(1)
catalog = sys.argv[1].lower()
new_zone = sys.argv[2]
if new_zone[-1] == '.':
new_zone = new_zone[:-1]
with open(catalog, 'r') as c:
for ln in c:
ln = ln[:-1].split('\t')
if ln[3] == 'SOA':
cat_serial = int(ln[4].split()[2])
new_serial = serial if serial > cat_serial else cat_serial + 1
zones = set()
with open(zonelistfile, 'r') as zl:
for ln in zl:
if not ln.startswith('add'):
continue
add, zone, pattern = ln.split()
if pattern == catalog:
zones.add(zone)
if 'add' in sys.argv[0]:
if os.system('nsd-control addzone %s %s' % (new_zone.lower(), catalog)) != 0:
sys.exit(1)
else:
zones.add(new_zone.lower())
elif new_zone.lower() not in zones:
print('Zone %s not found in catalog %s\n' % (new_zone, catalog))
sys.exit(1)
elif os.system('nsd-control delzone %s' % new_zone.lower()) != 0:
sys.exit(1)
else:
zones.remove(new_zone.lower())
with open(catalog, 'w') as c:
c.write( '%s.\t0\tIN\tSOA\tinvalid. invalid. %d 3600 600 2419200 0\n'
% (catalog, new_serial))
c.write('%s.\t0\tIN\tNS\tinvalid.\n' % catalog)
c.write('version.%s.\t0\tIN\tTXT\t"2"\n' % catalog)
for zone in zones:
c.write( '%s.zones.%s.\t0\tIN\tPTR\t%s.\n'
% ( uuid.uuid5(uuid.NAMESPACE_DNS, zone), catalog, zone ))
os.system('nsd-control reload %s' % catalog)
print('%s: %s' % (catalog, ' '.join(sorted(zones))))