Welcome, guest | Sign In | My Account | Store | Cart

Generates reverse DNS zone files for a given subnet.

Python, 63 lines
 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
import re
import sys

def help():
    print sys.argv[0]+" generates reverse DNS zonefiles for a given subnet."
    print "Usage "+sys.argv[0]+" [ipblock]"
    print "Example: "+sys.argv[0]+" 10.0.0.0/10"

if len(sys.argv) is not 2:
    help()
    sys.exit(1)

subnet = sys.argv[1]

subnet_checker = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}')
n_grps = re.compile(r'\d{1,3}')

is_subnet = subnet_checker.match(subnet)
if not is_subnet:
    help()
    sys.exit(1)

groups = n_grps.findall(subnet)
hbits = int(groups[(len(groups) - 1)])

if hbits < 8:
    help()
    sys.exit(1)
elif hbits > 32:
    help()
    sys.exit(1)

def bit2dec(bit):
    btable = {1:1, 2:2, 3:4, 4:8, 5:16, 6:32, 7:64, 8:128}
    dec = 0
    for x in range(bit):
        dec += btable[x+1]         
    return int(dec)

if __name__ == "__main__":
    bits = 32 - hbits
    hbits16 = int(groups[(len(groups) - 1)])
    if bits <= 8:
        zonefile = open(groups[0]+'.'+groups[1]+'.'+groups[2]+'.db', 'w')
        for x in range(int(groups[3]),(int(groups[3])+bit2dec(bits))):
            rdns = str(x)+'.'+ groups[2]+'.'+groups[1]+'.'+groups[0]+'.'+'IN-ADDR.ARPA\n'
            zonefile.write(rdns)
        zonefile.close()
    elif bits <= 16:
        for x in range(int(groups[2]), bit2dec(bits - 8)):
            zonefile = open(groups[0]+'.'+groups[1]+'.'+str(x)+'.db', 'w')
            for y in range(1, 256):
                rdns = str(y)+'.'+str(x)+'.'+groups[1]+'.'+groups[0]+'.'+'IN-ADDR.ARPA\n'
                zonefile.write(rdns)
            zonefile.close()
    elif bits <= 24:
        for x in range(int(groups[1]), bit2dec(bits - 16)):
            for y in range(1, 256):
                zonefile = open(groups[0]+'.'+str(x)+'.'+str(y)+'.db', 'w')
                for z in range(1, 256):
                    rdns = str(z)+'.'+str(y)+'.'+str(x)+'.'+groups[0]+'.'+'IN-ADDR.ARPA\n'
                    zonefile.write(rdns)
                zonefile.close()

Useful when one has to generate zonefiles for a /15 or /10 IP block, like in an ISP's DNS servers.