Skip to content

Instantly share code, notes, and snippets.

@alcemirfernandes
Forked from pklaus/rdns.py
Created October 31, 2013 19:19
Show Gist options
  • Save alcemirfernandes/7255261 to your computer and use it in GitHub Desktop.
Save alcemirfernandes/7255261 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
""" This is a Python script that helps you create reverse DNS zone files for the Bind Name Server.
I published it in my blog post http://goo.gl/CJwly ."""
## This script needs IPy.py:
## wget https://raw.github.com/haypo/python-ipy/master/IPy.py --no-check-certificate
from IPy import IP
class cIP (IP):
def __or__(self, other):
if self.ip & other.ip >0:
raise ValueError("Bitwise or is not defined for overlapping networks & identifiers.")
return cIP(self.ip | other.ip)
## configure your RDNS generation here:
host = "example.com."
first_name_server = "ns1.example.com."
administrative_contact = "admin.example.com."
subnet = cIP("2001:db8::/32")
rdns_entries = []
# a list of RDNS entries which are of the form (IPv6 Address , Fully Qualified Domain Name)
rdns_entries.append( ( subnet | cIP("::1"), "host1."+host ) )
rdns_entries.append( ( subnet | cIP("::2"), "host2."+host ) )
## should not need to modify:
record_ttl = "1h"
from datetime import datetime
zone_serial = datetime.now().strftime("%Y%m%d%H%M%S")
slave_refresh_interval = "1h"
slave_retry_interval = "15m"
slave_expiration_time = "1w"
nxdomain_cache_time = "1h"
### Begin of the output generation
print("; Zone file built with the Python Tool rdns.py:")
print("; " + __doc__.replace("\n","\n; ") )
print("$TTL %s ; Default TTL" % record_ttl )
print("@ IN SOA %s %s (" % (first_name_server, administrative_contact) )
print(" %s ; serial" % zone_serial)
print(" %s ; slave refresh interval" % slave_refresh_interval)
print(" %s ; slave retry interval" % slave_retry_interval)
print(" %s ; slave copy expire time" % slave_expiration_time)
print(" %s ; NXDOMAIN cache time" % nxdomain_cache_time)
print(" )")
print("; domain name servers")
print("@ IN NS %s" % first_name_server)
print("; IPv6 PTR entries")
for rdns_entry in rdns_entries:
print("%s IN PTR %s" % (rdns_entry[0].reverseNames()[0], rdns_entry[1]) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment