Skip to content

Instantly share code, notes, and snippets.

@s4mdf0o1
Last active November 1, 2016 16:53
Show Gist options
  • Save s4mdf0o1/e02a8b6ebdacd0f74a8b834479e17a7a to your computer and use it in GitHub Desktop.
Save s4mdf0o1/e02a8b6ebdacd0f74a8b834479e17a7a to your computer and use it in GitHub Desktop.
Change your authoritative DNS Serial
#!/usr/bin/env python
import os
import re
import time
file = "<put/your/db.domain.tld/zone/file/here>"
date = time.strftime("%Y%m%d")
regex = r"^( *)(20[0-9]{6})([0-9]{2}) ; Serial"
new_date = ""
with open(file) as fp:
with open(file+'.tmp','w') as dest:
for line in fp.readlines():
if re.match(regex, line):
#print "match"
rgx = re.match(regex, line)
nb_spaces = rgx.group(1)
last_date = rgx.group(2)
last_serial = rgx.group(3)
if last_date != date:
new_date = date
serial="01"
else:
new_date = last_date
serial = int(last_serial) + 1
if serial < 10:
serial = "%02d" % (serial,)
print nb_spaces + new_date + serial + ' ; Serial'
dest.write(nb_spaces + new_date + serial + ' ; Serial'+'\n')
else:
#pass
print line.strip('\n')
dest.write(line)
print "$ diff <put/your/db.domain.tld/zone/file/here>{,.tmp}"
@s4mdf0o1
Copy link
Author

s4mdf0o1 commented Nov 1, 2016

Your file dns-db-zone, should look like :

$ORIGIN domain.tld.
$TTL 7200

; SOA

@       IN      SOA    ns1.domain.tld. hostmaster.domain.tld. (
                                        2016110103 ; Serial
                                        7200       ; Refresh
                                        1800       ; Retry
                                        1209600    ; Expire
                                        86400 )    ; Minimum

; NAMESERVERS
@    IN    NS    ns1.domain.tld.

; A RECORDS
@            IN    A    xx.xxx.xxx.xx
nethost      IN    A    xx.xxx.xxx.xx
ns1          IN    A    xx.xxx.xxx.xx
home         IN    A    xx.xxx.xxx.xx
[...]

; CNAME RECORDS
www         IN    CNAME    nethost
jabber      IN    CNAME    nethost
[...]

for bind9/nsd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment