<?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>diginc &#187; python</title>
	<atom:link href="http://diginc.us/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://diginc.us</link>
	<description>\'dij-iŋk\</description>
	<lastBuildDate>Thu, 19 May 2011 14:49:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using IPTables with Dynamic IP hostnames like dyndns.org</title>
		<link>http://diginc.us/linux/2010/using-iptables-with-dynamic-ip-hostnames-like-dyndns-org/</link>
		<comments>http://diginc.us/linux/2010/using-iptables-with-dynamic-ip-hostnames-like-dyndns-org/#comments</comments>
		<pubDate>Wed, 26 May 2010 15:51:42 +0000</pubDate>
		<dc:creator>diginc</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://diginc.us/?p=172</guid>
		<description><![CDATA[Whenever IPTables has a hostname in a rule it looks up the hostname&#8217;s IP address and uses that instead of the actual hostname &#8211; so it&#8217;s stuck with the IP until the next time IPTables is flushed/restarted. Here&#8217;s a quick &#8230; <a href="http://diginc.us/linux/2010/using-iptables-with-dynamic-ip-hostnames-like-dyndns-org/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whenever IPTables has a hostname in a rule it looks up the hostname&#8217;s IP address and uses that instead of the actual hostname &#8211; so it&#8217;s stuck with the IP until the next time IPTables is flushed/restarted.  Here&#8217;s a quick little python script to stick in a crontab which checks the IP of your dynamic IP hostname (free ones provided by dyndns.org) and will restart iptables if it catches a change in your hostname.  The script was made for CentOS so should work on Red Hat based distributions &#8211; if you don&#8217;t have an /etc/init.d/iptables file you&#8217;ll have to modify the reload iptables command in the source.  Viewable Source After Jump</p>
<p>I just set this up as root and in root&#8217;s crontab.</p>
<p><a href="/examples/iptables_dyndns_update.py">Download Source</a></p>
<p><span id="more-172"></span></p>
<p><strong>Source:</strong></p>

<div class="wp_codebox"><table><tr id="p1723"><td class="code" id="p172code3"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> gettextoutput<span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Return (status, output) of executing cmd in a shell.&quot;&quot;&quot;</span>
    pipe = <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'{ '</span> + <span style="color: #dc143c;">cmd</span> + <span style="color: #483d8b;">'; } 2&gt;&amp;1'</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
    pipe = <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span> + <span style="color: #483d8b;">' 2&gt;&amp;1'</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
    text = pipe.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> text<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>: text = text<span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> text
&nbsp;
home_dyndns = <span style="color: #483d8b;">&quot;example.dyndns.org&quot;</span>
log_dyndns = <span style="color: #483d8b;">&quot;./new_home_ip_check.log&quot;</span>
last_dyndns = gettextoutput<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cat &quot;</span> + log_dyndns<span style="color: black;">&#41;</span>
cur_dyndns = gettextoutput<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;host &quot;</span> + home_dyndns<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Log: &quot;</span>+ last_dyndns
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Cur: &quot;</span>+ cur_dyndns
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> last_dyndns == cur_dyndns:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPs match, no restart necessary&quot;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Updating last IP with current&quot;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;echo '&quot;</span> + cur_dyndns + <span style="color: #483d8b;">&quot;' &gt; &quot;</span> + log_dyndns<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Restarting iptables to update&quot;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/etc/init.d/iptables restart&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Output looks like:</strong></p>

<div class="wp_codebox"><table><tr id="p1724"><td class="code" id="p172code4"><pre class="bash" style="font-family:monospace;">Log: example.dyndns.org has address 114.76.37.112
Cur: example.dyndns.org has address 114.76.37.112
IPs match, no restart necessary
&nbsp;
Log: example.dyndns.org has address 114.76.37.113
Cur: example.dyndns.org has address 114.76.37.112
Updating <span style="color: #c20cb9; font-weight: bold;">last</span> IP with current
Restarting iptables to update
Flushing firewall rules:                                   <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span>
Setting chains to policy ACCEPT: filter                    <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span>
Unloading iptables modules:                                <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span>
Applying iptables firewall rules:                          <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span>
Loading additional iptables modules: ip_conntrack_netbios_n<span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://diginc.us/linux/2010/using-iptables-with-dynamic-ip-hostnames-like-dyndns-org/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: diginc.us @ 2012-02-06 01:05:14 -->
