summaryrefslogtreecommitdiff
path: root/examples/reverse.py
diff options
context:
space:
mode:
authorBob Halley <halley@dnspython.org>2005-09-02 05:21:28 +0000
committerBob Halley <halley@dnspython.org>2005-09-02 05:21:28 +0000
commitdf24d7e7fe18b2a4cd79c35d1c2efbb3e7ee5abc (patch)
tree3a987bc61f9847fb2d344d4d1fdbc56fa8f6bff9 /examples/reverse.py
parent2ed5e08d4485c5df41f6f2ef3b04148ef10af1cb (diff)
downloaddnspython-df24d7e7fe18b2a4cd79c35d1c2efbb3e7ee5abc.tar.gz
initial import
Original author: Bob Halley <halley@dnspython.org> Date: 2004-03-23 21:57:40
Diffstat (limited to 'examples/reverse.py')
-rwxr-xr-xexamples/reverse.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/reverse.py b/examples/reverse.py
new file mode 100755
index 0000000..47facc7
--- /dev/null
+++ b/examples/reverse.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+# Usage: reverse.py <zone_filename>...
+#
+# This demo script will load in all of the zones specified by the
+# filenames on the command line, find all the A RRs in them, and
+# construct a reverse mapping table that maps each IP address used to
+# the list of names mapping to that address. The table is then sorted
+# nicely and printed.
+#
+# Note! The zone name is taken from the basename of the filename, so
+# you must use filenames like "/wherever/you/like/dnspython.org" and
+# not something like "/wherever/you/like/foo.db" (unless you're
+# working with the ".db" GTLD, of course :)).
+#
+# If this weren't a demo script, there'd be a way of specifying the
+# origin for each zone instead of constructing it from the filename.
+
+import dns.zone
+import dns.ipv4
+import os.path
+import sys
+
+reverse_map = {}
+
+for filename in sys.argv[1:]:
+ zone = dns.zone.from_file(filename, os.path.basename(filename),
+ relativize=False)
+ for (name, ttl, rdata) in zone.iterate_rdatas('A'):
+ l = reverse_map.get(rdata.address)
+ if l is None:
+ l = []
+ reverse_map[rdata.address] = l
+ l.append(name)
+
+keys = reverse_map.keys()
+keys.sort(lambda a1, a2: cmp(dns.ipv4.inet_aton(a1), dns.ipv4.inet_aton(a2)))
+for k in keys:
+ v = reverse_map[k]
+ v.sort()
+ l = map(str, v) # convert names to strings for prettier output
+ print k, l