|
| 1 | +from __future__ import print_function |
1 | 2 | # This is a hack I use to generate my tinydns configuration |
2 | 3 | # It serves as e test for converting from Perl Net::IP to |
3 | 4 | # Python and IPy |
|
15 | 16 | 'ns.dorsch.org': '195.143.234.25', |
16 | 17 | 'ns.c0re.jp': '217.6.214.130'} |
17 | 18 |
|
18 | | -print "# *** nameservers ***" |
19 | | -for x in ns.keys(): |
20 | | - print "=%s:%s" % (x, ns[x]) |
| 19 | +print("# *** nameservers ***") |
| 20 | +for x in ns: |
| 21 | + print("=%s:%s" % (x, ns[x])) |
21 | 22 |
|
22 | | -print "\n# *** domains ***" |
| 23 | +print("\n# *** domains ***") |
23 | 24 |
|
24 | 25 | fd = open('domains') |
25 | 26 |
|
|
28 | 29 | if x[-1] == '\n': |
29 | 30 | x = x[:-1] |
30 | 31 | (domain, owner) = x.split(':') |
31 | | - print "'%s:Contact for this domain is %s" % (domain, owner) |
32 | | - for y in ns.keys(): |
33 | | - print ".%s::%s" % (domain, y) |
| 32 | + print("'%s:Contact for this domain is %s" % (domain, owner)) |
| 33 | + for y in ns: |
| 34 | + print(".%s::%s" % (domain, y)) |
34 | 35 |
|
35 | 36 | fd.close() |
36 | 37 |
|
37 | | -print "\n# *** Networks ***" |
| 38 | +print("\n# *** Networks ***") |
38 | 39 |
|
39 | 40 | fd = open('networks') |
40 | 41 | ip6map = {} |
|
47 | 48 | if len(x) > 0 and x[0] != '#': |
48 | 49 | nets = x.split(',') |
49 | 50 | name = nets.pop(0) |
50 | | - print "# Network: %s" % name |
| 51 | + print("# Network: %s" % name) |
51 | 52 | for y in nets: |
52 | 53 | ip = IPy.IP(y) |
53 | | - print "# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len()) |
54 | | - print "=net.%s:%s" % (name, ip.net()) |
55 | | - print "=broadcast.%s:%s" % (name, ip.broadcast()) |
| 54 | + print("# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len())) |
| 55 | + print("=net.%s:%s" % (name, ip.net())) |
| 56 | + print("=broadcast.%s:%s" % (name, ip.broadcast())) |
56 | 57 |
|
57 | 58 | if ip.version() == 4: |
58 | 59 | for z in ip: |
|
61 | 62 | rmap[z.int()] = z.strBin() + "." + name |
62 | 63 | else: |
63 | 64 | # IPv6 |
64 | | - for z in ns.keys(): |
| 65 | + for z in ns: |
65 | 66 | for v in ip.reverseName(): |
66 | | - print ".%s::%s" % (v, z) |
| 67 | + print(".%s::%s" % (v, z)) |
67 | 68 | ip6map[ip.strFullsize(0)] = name |
68 | 69 |
|
69 | 70 | fd.close() |
70 | 71 |
|
71 | | -print "\n# *** hosts ***" |
| 72 | +print("\n# *** hosts ***") |
72 | 73 |
|
73 | 74 | fd = open('hosts') |
74 | 75 |
|
|
77 | 78 | x = x[:-1] |
78 | 79 | if x != '' and x[0] != '#': |
79 | 80 | if "@Z'.".find(x[0]) >= 0: |
80 | | - print x |
| 81 | + print(x) |
81 | 82 | else: |
82 | 83 | if "=+'".find(x[0]) >= 0: |
83 | 84 | i = x.split(':') |
84 | 85 | rmap[IPy.IP(i[1]).int()] = '' |
85 | | - print x |
| 86 | + print(x) |
86 | 87 | else: |
87 | 88 | x = x[1:] |
88 | 89 | x += '||||' |
|
107 | 108 | ip = IPy.IP(y) |
108 | 109 | if ip.version() == 4: |
109 | 110 | # IPv4 is easy |
110 | | - if not nmap.has_key(ip.int()): |
111 | | - print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (y, name) |
112 | | - print "# no network for %s (%s)" % (y, name) |
| 111 | + if ip.int() not in nmap: |
| 112 | + print("*** warning: no network for %s (%s) - ignoring" % (y, name), file=sys.stderr) |
| 113 | + print("# no network for %s (%s)" % (y, name)) |
113 | 114 | else: |
114 | | - print "=%s.%s:%s" % (name, nmap[ip.int()], y) |
115 | | - print "'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin) |
| 115 | + print("=%s.%s:%s" % (name, nmap[ip.int()], y)) |
| 116 | + print("'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin)) |
116 | 117 | rmap[ip.int()] = '' |
117 | 118 | for z in aliases: |
118 | | - print "+%s:%s" % (z, ip) |
119 | | - print "'%s:Host contact is %s" % (z, admin) |
| 119 | + print("+%s:%s" % (z, ip)) |
| 120 | + print("'%s:Host contact is %s" % (z, admin)) |
120 | 121 | else: |
121 | 122 | #IPv6 here |
122 | 123 | net = ip.strFullsize(0) |
123 | 124 | net = net[:19] + ':0000:0000:0000:0000' |
124 | | - if ip6map.has_key(net): |
125 | | - print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (ip, name) |
126 | | - print "# no network for %s (%s) - ignoring" % (ip, name) |
| 125 | + if net in ip6map: |
| 126 | + print("*** warning: no network for %s (%s) - ignoring" % (ip, name), file=sys.stderr) |
| 127 | + print("# no network for %s (%s) - ignoring" % (ip, name)) |
127 | 128 | else: |
128 | | - print "6%s.%s:%s"; (name, ip6map[net], ip.strHex()[2:]) |
| 129 | + print("6%s.%s:%s" % (name, ip6map[net], ip.strHex()[2:])) |
129 | 130 | for z in aliases: |
130 | | - print "3%s:%s" % (name, ip.strHex()[2:]) |
131 | | - print "'%s:Host contact is %s" % (name, admin) |
| 131 | + print("3%s:%s" % (name, ip.strHex()[2:])) |
| 132 | + print("'%s:Host contact is %s" % (name, admin)) |
132 | 133 |
|
133 | 134 | fd.close() |
134 | 135 |
|
135 | | -print "\n# *** reverse lookup ***" |
136 | | -k = nmap.keys() |
137 | | -k.sort() |
138 | | -for x in k: |
139 | | - if rmap.has_key(x) and rmap[x] != '': |
140 | | - print "=%s:%s" % (rmap[x], str(IPy.IP(x))) |
| 136 | +print("\n# *** reverse lookup ***") |
| 137 | +for x in sorted(nmap): |
| 138 | + if rmap.get(x): |
| 139 | + print("=%s:%s" % (rmap[x], str(IPy.IP(x)))) |
141 | 140 |
|
0 commit comments