<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Local Loop &#187; bind</title>
	<atom:link href="http://localloop.co.za/tag/bind/feed/" rel="self" type="application/rss+xml" />
	<link>http://localloop.co.za</link>
	<description>Internet and Networking in South Africa</description>
	<lastBuildDate>Wed, 18 Apr 2012 12:27:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Making BIND zone files readable</title>
		<link>http://localloop.co.za/2008/09/making-bind-zone-files-readable/</link>
		<comments>http://localloop.co.za/2008/09/making-bind-zone-files-readable/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 09:11:50 +0000</pubDate>
		<dc:creator>Simeon Miteff</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[dns]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://localloop.co.za/?p=76</guid>
		<description><![CDATA[Update: Added MIT License The zone configuration format for BIND has a number of short-cuts that make writing DNS zone files easy, but also make them hard to read. Here is an example: $TTL 3D @ SOA server1.example.com. hostmaster.example.com. (1 8H 2H 4W 1D) NS server1 NS server2 MX 10 server1 MX 20 server2 server1 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: Added MIT License</strong></p>
<p>The zone configuration format for <a href="http://www.isc.org/sw/bind/">BIND</a> has a number of short-cuts that make writing DNS zone files easy, but also make them hard to read. Here is an example:<br />
<code><br />
$TTL 3D<br />
@   SOA server1.example.com. hostmaster.example.com. (1 8H 2H 4W 1D)<br />
    NS  server1<br />
    NS  server2<br />
    MX 10   server1<br />
    MX 20   server2<br />
server1  A   127.0.0.1<br />
server2  A   127.0.0.2<br />
www     CNAME   server1<br />
</code></p>
<p><a href="http://www.python.org">Python</a> to the rescue. The <a href="http://www.dnspython.com">dnspython</a> module has support for parsing BIND zones, and rendering them without the shortcuts:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># bindnorm.py - &quot;normalise&quot; a bind zone file by expanding relative names.</span>
<span style="color: #808080; font-style: italic;"># Simeon Miteff &lt;simeon@localloop.co.za&gt;</span>
<span style="color: #808080; font-style: italic;"># Tue Sep 23 10:03:35 SAST 2008</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#Copyright (c) 2008 Simeon Miteff</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span style="color: #808080; font-style: italic;">#of this software and associated documentation files (the &quot;Software&quot;), to </span>
<span style="color: #808080; font-style: italic;">#deal in the Software without restriction, including without limitation the </span>
<span style="color: #808080; font-style: italic;">#rights to use, copy, modify, merge, publish, distribute, sublicense, and/or </span>
<span style="color: #808080; font-style: italic;">#sell copies of the Software, and to permit persons to whom the Software is </span>
<span style="color: #808080; font-style: italic;">#furnished to do so, subject to the following conditions:</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#The above copyright notice and this permission notice shall be included in </span>
<span style="color: #808080; font-style: italic;">#all copies or substantial portions of the Software.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;">#THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR </span>
<span style="color: #808080; font-style: italic;">#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, </span>
<span style="color: #808080; font-style: italic;">#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL </span>
<span style="color: #808080; font-style: italic;">#THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER </span>
<span style="color: #808080; font-style: italic;">#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING </span>
<span style="color: #808080; font-style: italic;">#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS </span>
<span style="color: #808080; font-style: italic;">#IN THE SOFTWARE.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> dns.<span style="color: black;">zone</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">2</span>:
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Usage: %s [zone fqdn]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Example: %s example.com &lt; db.example.com &gt; norm.db.example.com<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
zone = dns.<span style="color: black;">zone</span>.<span style="color: black;">from_text</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, relativize=<span style="color: #008000;">False</span>, check_origin=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
zone.<span style="color: black;">to_file</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>, relativize=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span></pre></div></div>

<p>To use it, you&#8217;ll need to install <code>dnspython</code> first. Fortunately, on Debian or Ubuntu, it&#8217;s just an <code>apt-get install python-dnspython</code> away. Running this little script with the example zone above as input yields:<br />
<code><br />
example.com. 259200 IN SOA server1.example.com. hostmaster.example.com. 1 28800 7200 2419200 86400<br />
example.com. 259200 IN NS server1.example.com.<br />
example.com. 259200 IN NS server2.example.com.<br />
example.com. 259200 IN MX 10 server1.example.com.<br />
example.com. 259200 IN MX 20 server2.example.com.<br />
server1.example.com. 259200 IN A 127.0.0.1<br />
server2.example.com. 259200 IN A 127.0.0.2<br />
www.example.com. 259200 IN CNAME server1.example.com.<br />
</code></p>
<p>This format is less canonical, but at least each line is context-free. Now you can proceed to <code>grep</code> the output to your heart&#8217;s desire :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://localloop.co.za/2008/09/making-bind-zone-files-readable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

