File: confbuilder.py

package info (click to toggle)
ipy 1%3A0.52-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 204 kB
  • ctags: 200
  • sloc: python: 1,350; makefile: 27
file content (141 lines) | stat: -rw-r--r-- 4,582 bytes parent folder | download | duplicates (5)
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
# This is a hack I use to generate my tinydns configuration
# It serves as e test for converting from Perl Net::IP to
# Python and IPy

# Further Information might be available at http://c0re.jp/c0de/IPy/
# Hacked 2001 by drt@un.bewaff.net

import sys
sys.path.append('..')

import IPy


ns = {'ns.nerxs.com': '213.221.113.70',
      'ns.dorsch.org': '195.143.234.25',
      'ns.c0re.jp': '217.6.214.130'}

print "# *** nameservers ***"
for x in ns.keys():
    print "=%s:%s" % (x, ns[x])

print "\n# *** domains ***"

fd = open('domains')

for x in fd.readlines():
    if x[0] != '#':
        if x[-1] == '\n':
            x = x[:-1]
        (domain, owner) = x.split(':')
        print "'%s:Contact for this domain is %s" % (domain, owner)
        for y in ns.keys(): 
            print ".%s::%s" % (domain, y)

fd.close()

print "\n# *** Networks ***"

fd = open('networks')
ip6map = {}
rmap = {}
nmap = {}

for x in fd.readlines():
    if x[-1] == '\n':
        x = x[:-1]
    if len(x) > 0 and x[0] != '#':
        nets = x.split(',')
        name = nets.pop(0)
        print "# Network: %s" % name
        for y in nets:
            ip = IPy.IP(y)
            print "# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len())
            print "=net.%s:%s" % (name, ip.net())
            print "=broadcast.%s:%s" % (name, ip.broadcast())
            
            if ip.version() == 4:
                for z in ip:
                    # TODO reverse?
                    nmap[z.int()] = name
                    rmap[z.int()] = z.strBin() + "." + name
            else:
                # IPv6
                for z in ns.keys():
                    for v in ip.reverseName():
                        print ".%s::%s" % (v, z) 
                ip6map[ip.strFullsize(0)] = name

fd.close()

print "\n# *** hosts ***"
      
fd = open('hosts')

for x in fd.readlines():
    if x[-1] == '\n':
        x = x[:-1]
    if x != '' and x[0] != '#':
        if "@Z'.".find(x[0]) >= 0:
            print x
        else:
            if "=+'".find(x[0]) >= 0:
                i = x.split(':')
                rmap[IPy.IP(i[1]).int()] = ''
                print x
            else:
                x = x[1:]
                x += '||||'
                fields = x.split('|')
                name = fields.pop(0)
                if name[0] == '.':
                    name = name[1:]
                v = fields.pop(0)
                ips = v.split(',')
                v = fields.pop(0)
                aliases = v.split(',')
                if aliases == ['']:
                    aliases = []
                admin = fields.pop()
                if admin == '':
                    admin = 'technik@c0re.23.nu'
                v = fields.pop()
                mxes = v.split(',')
                if mxes == ['']:
                    mxes = []
                for y in ips:
                    ip = IPy.IP(y) 
                    if ip.version() == 4:
                        # IPv4 is easy
                        if not nmap.has_key(ip.int()):
                            print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (y, name)
                            print "# no network for %s (%s)" % (y, name)
                        else:
                            print "=%s.%s:%s" % (name, nmap[ip.int()], y)
                            print "'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin)
                            rmap[ip.int()] = ''      
                            for z in aliases:
                                print "+%s:%s" % (z, ip)
                                print "'%s:Host contact is %s" % (z, admin)
                    else:
                        #IPv6 here
                        net = ip.strFullsize(0)
                        net = net[:19] + ':0000:0000:0000:0000'
                        if ip6map.has_key(net):
                            print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (ip, name)
                            print "# no network for %s (%s) - ignoring" % (ip, name)
                        else:  
                            print "6%s.%s:%s"; (name, ip6map[net], ip.strHex()[2:])
                            for z in aliases:
                                print "3%s:%s" % (name, ip.strHex()[2:])
                                print "'%s:Host contact is %s" % (name, admin)

fd.close()

print "\n# *** reverse lookup ***"
k = nmap.keys()
k.sort()
for x in k:
    if rmap.has_key(x) and rmap[x] != '':
      print "=%s:%s" % (rmap[x], str(IPy.IP(x)))