<?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>Kunal Dua &#187; unix</title>
	<atom:link href="http://www.kunaldua.com/blog/category/unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kunaldua.com/blog</link>
	<description>Laziness. Impatience. Hubris.</description>
	<lastBuildDate>Thu, 11 Aug 2011 12:54:43 +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>Bespin Gotchas</title>
		<link>http://www.kunaldua.com/blog/2010/06/bespin-gotchas/</link>
		<comments>http://www.kunaldua.com/blog/2010/06/bespin-gotchas/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 11:10:23 +0000</pubDate>
		<dc:creator>Kunal</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[www]]></category>

		<guid isPermaLink="false">http://www.kunaldua.com/blog/2010/06/bespin-gotchas/</guid>
		<description><![CDATA[In case of trouble connecting to Bespin from an external machine, try setting the IP address to 0.0.0.0 Either change the file pavement.py: 62 63 64 65 66 67 68 69 70 71 72 server=Bunch&#40; # set to true to &#8230; <a href="http://www.kunaldua.com/blog/2010/06/bespin-gotchas/">Continue reading <span class="meta-nav">&#8594;</span></a>


Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/' rel='bookmark' title='Get email updates when your IP changes/ Python DynDns update client'>Get email updates when your IP changes/ Python DynDns update client</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In case of trouble connecting to Bespin from an external machine, try setting the IP address to 0.0.0.0</p>
<p>Either change the file pavement.py:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>62
63
64
65
66
67
68
69
70
71
72
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">server=Bunch<span style="color: black;">&#40;</span>
   <span style="color: #808080; font-style: italic;"># set to true to allow connections from other machines</span>
   address=<span style="color: #483d8b;">&quot;0.0.0.0&quot;</span>,
   port=<span style="color: #483d8b;">&quot;8080&quot;</span>,
   try_build=<span style="color: #008000;">False</span>,
   dburl=<span style="color: #008000;">None</span>,
   async=<span style="color: #008000;">False</span>,
   config_file=path<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;devconfig.py&quot;</span><span style="color: black;">&#41;</span>,
   directory=path<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;../bespinserver/&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
   clientdir=path.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>Or pass it as a command line argument:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">paver server.address=0.0.0.0 server.port=<span style="color: #000000;">8080</span> start</pre></div></div>

<p>In case you haven&#8217;t noticed, you can even specify the port to start on i.e. run Bespin on a custom port.</p>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.kunaldua.com/blog/2010/06/bespin-gotchas/' addthis:title='Bespin Gotchas '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>

<p>Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/' rel='bookmark' title='Get email updates when your IP changes/ Python DynDns update client'>Get email updates when your IP changes/ Python DynDns update client</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kunaldua.com/blog/2010/06/bespin-gotchas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sorted, Human Readable file sizes via du</title>
		<link>http://www.kunaldua.com/blog/2010/06/sorted-human-readable-file-sizes-via-du/</link>
		<comments>http://www.kunaldua.com/blog/2010/06/sorted-human-readable-file-sizes-via-du/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 05:14:51 +0000</pubDate>
		<dc:creator>Kunal</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.kunaldua.com/blog/2010/06/sorted-human-readable-file-sizes-via-du/</guid>
		<description><![CDATA[du -d 1 &#124; sort -n &#124; cut -f 2 &#124; xargs du -h -d 0 No related posts.


No related posts.]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-d</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-h</span> <span style="color: #660033;">-d</span> <span style="color: #000000;">0</span></pre></div></div>

<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.kunaldua.com/blog/2010/06/sorted-human-readable-file-sizes-via-du/' addthis:title='Sorted, Human Readable file sizes via du '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.kunaldua.com/blog/2010/06/sorted-human-readable-file-sizes-via-du/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Get email updates when your IP changes/ Python DynDns update client</title>
		<link>http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/</link>
		<comments>http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/#comments</comments>
		<pubDate>Tue, 11 May 2010 05:15:36 +0000</pubDate>
		<dc:creator>Kunal</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[www]]></category>

		<guid isPermaLink="false">http://www.kunaldua.com/blog/?p=360</guid>
		<description><![CDATA[I use DynDns to map my ISP provided dynamic IP to a static hostname. For some reason, the DynDns provided update clients don&#8217;t always work for me and often leave my hostname pointing to a dead or (worse) someone else&#8217;s &#8230; <a href="http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/">Continue reading <span class="meta-nav">&#8594;</span></a>


Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/' rel='bookmark' title='Reliance Wireless broadband auto-login (and logout) script(s)'>Reliance Wireless broadband auto-login (and logout) script(s)</a></li>
<li><a href='http://www.kunaldua.com/blog/2005/03/yahoo-mail-reduce-the-spam-you-get/' rel='bookmark' title='Yahoo Mail &#8211; reduce the spam you get*'>Yahoo Mail &#8211; reduce the spam you get*</a></li>
<li><a href='http://www.kunaldua.com/blog/2010/06/bespin-gotchas/' rel='bookmark' title='Bespin Gotchas'>Bespin Gotchas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I use <a href="http://www.dyndns.com/" target="_blank">DynDns</a> to map my ISP provided dynamic IP to a static hostname. For some reason, the DynDns provided <a href="http://www.dyndns.com/support/clients/" target="_blank">update clients</a> don&#8217;t always work for me and often leave my hostname pointing to a dead or (worse) someone else&#8217;s IP. I decided to take matter into my own hands and write a script that would email me my IP whenever my DHCP lease expired and my ISP issued me a fresh one. This would ensure I know how to reach back home, even if my hostname was pointing to an old IP.</p>
<p>While going through the DynDns API, I realized it was trivial to update the hostname as well, essentially replicating the functionality of the aforementioned client(s). So I decided to add that as well.</p>
<p>I know this functionality can be replicated via curl + sendmail, but Python is my tool of choice, so just live with it. Without further ado, here&#8217;s the script, with an explanation afterwards.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># encoding: utf-8</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Script to email IP whenever it changes. Also updates DynDns hostname.
Version 1.0
&nbsp;
Created by Kunal Dua on 2010-05-10
http://www.kunaldua.com/blog/?p=360
&nbsp;
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> send_mail<span style="color: black;">&#40;</span>subject, content<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">smtplib</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">mime</span>.<span style="color: black;">text</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEText
        SERVER = <span style="color: #483d8b;">&quot;smtpserver&quot;</span>
        PORT = <span style="color: #ff4500;">587</span>  <span style="color: #808080; font-style: italic;">#Use 25 if this doesn't work</span>
        USER = <span style="color: #483d8b;">&quot;username&quot;</span>
        PASS = <span style="color: #483d8b;">&quot;password&quot;</span>
        FROM = <span style="color: #483d8b;">&quot;IPBot &lt;mail@example.com&gt;&quot;</span>
        TO = <span style="color: #483d8b;">&quot;user+folder@example.com&quot;</span>
&nbsp;
        SUBJECT = subject
        TEXT = content
&nbsp;
        message = MIMEText<span style="color: black;">&#40;</span>TEXT<span style="color: black;">&#41;</span>
        message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Subject'</span><span style="color: black;">&#93;</span> = SUBJECT
        message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'From'</span><span style="color: black;">&#93;</span> = FROM
        message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'To'</span><span style="color: black;">&#93;</span> = TO
&nbsp;
        server = <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTP</span><span style="color: black;">&#40;</span>SERVER, PORT<span style="color: black;">&#41;</span>
        server.<span style="color: black;">login</span> <span style="color: black;">&#40;</span>USER, PASS<span style="color: black;">&#41;</span>
        server.<span style="color: black;">sendmail</span><span style="color: black;">&#40;</span>FROM, TO, message.<span style="color: black;">as_string</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        server.<span style="color: black;">quit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> update_dyndns<span style="color: black;">&#40;</span>theip<span style="color: black;">&#41;</span>:
        USERNAME = <span style="color: #483d8b;">'username'</span>
        PASSWORD = <span style="color: #483d8b;">'password'</span>
        HOSTNAME = <span style="color: #483d8b;">'example.dyndns.org'</span>
&nbsp;
        theurl = <span style="color: #483d8b;">'https://%s:%s@members.dyndns.org/nic/update?hostname=%s&amp;myip=%s&amp;wildcard=NOCHG&amp;mx=NOCHG&amp;backmx=NOCHG'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>USERNAME, PASSWORD, HOSTNAME, theip<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
        conn = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>theurl<span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;">#print conn.read()</span>
        conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>, <span style="color: #dc143c;">re</span>
        conn = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://checkip.dyndns.com/'</span><span style="color: black;">&#41;</span>
        data = conn.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        m = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'([0-9]*)(<span style="color: #000099; font-weight: bold;">\.</span>)([0-9]*)(<span style="color: #000099; font-weight: bold;">\.</span>)([0-9]*)(<span style="color: #000099; font-weight: bold;">\.</span>)([0-9]*)'</span>, data<span style="color: black;">&#41;</span>
        currip = m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
        lastfile = <span style="color: #483d8b;">&quot;lastip.txt&quot;</span>
        allfile = <span style="color: #483d8b;">&quot;history.txt&quot;</span>
&nbsp;
        theipfile = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>lastfile,<span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span>
        lastip = theipfile.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        theipfile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> lastip == currip:
                <span style="color: #808080; font-style: italic;">#print &quot;no change needed&quot;</span>
                exit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
                histfile = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>allfile, <span style="color: #483d8b;">&quot;a&quot;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
                thenow = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">ctime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                histfile.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>thenow, currip<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                histfile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                theipfile = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>lastfile,<span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
                theipfile.<span style="color: black;">write</span><span style="color: black;">&#40;</span>currip<span style="color: black;">&#41;</span>
                theipfile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                send_mail<span style="color: black;">&#40;</span>currip, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
                update_dyndns<span style="color: black;">&#40;</span>currip<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<ul>
<li>Lines 17-22 and 39-41 replace with your email and DynDns settings respectively.</li>
<li>Line 22 &#8211; My email provider supports redirecting mails to a folder by simply adding the name of the folder before @ sign. For example user+ipupdates@example.com will deliver mail in folder ipupdates of user@example.com. If your email provider supports this, it&#8217;s a useful trick to prevent these mails from cluttering up your inbox. If not, simply enter your regular email address.</li>
<li>Lines 60-61 initialize 2 files that I use. One is to store the current IP (or the last known IP) and the other is a history of all IP changes. The former is used to compare if the IP has changed since the script was last run and thus if an email needs to be sent + DynDns updated. The latter is not really needed for the script to function properly, and is used to maintain a log of all IP changes &#8211; because you can!</li>
<li>Line 78 &#8211; By default, the subject of the mail is the IP and the body/ text is blank. Feel free to obfuscate your IP if you feel paranoid about sending it in clear text or write sweet nothings to yourself in the body.</li>
<li><strong>Note:</strong> Before you run this script for the first time, create an empty file called lastip.txt in the same directory as the script or the script will fail. I know I could write a trivial check for this, but I leave that as an exercise for the reader.</li>
</ul>
<p>Recommended frequency of running this job via cron/ launchd is 10 minutes.</p>
<p><strong>Update</strong>: (May 27) I am pretty sure the DynDns API is broken in some way because I can&#8217;t get it to update even with this script. The email part is working pretty good for me though!</p>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/' addthis:title='Get email updates when your IP changes/ Python DynDns update client '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>

<p>Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/' rel='bookmark' title='Reliance Wireless broadband auto-login (and logout) script(s)'>Reliance Wireless broadband auto-login (and logout) script(s)</a></li>
<li><a href='http://www.kunaldua.com/blog/2005/03/yahoo-mail-reduce-the-spam-you-get/' rel='bookmark' title='Yahoo Mail &#8211; reduce the spam you get*'>Yahoo Mail &#8211; reduce the spam you get*</a></li>
<li><a href='http://www.kunaldua.com/blog/2010/06/bespin-gotchas/' rel='bookmark' title='Bespin Gotchas'>Bespin Gotchas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Reliance Wireless broadband auto-login (and logout) script(s)</title>
		<link>http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/</link>
		<comments>http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 09:55:51 +0000</pubDate>
		<dc:creator>Kunal</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[india]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[www]]></category>

		<guid isPermaLink="false">http://www.kunaldua.com/blog/?p=330</guid>
		<description><![CDATA[The old &#8220;curl&#8221; based method stopped working yesterday when Reliance got a new login page as well as a new backend. It seems Reliance is now also looking at Cookies during authentication. Here&#8217;s a little Python script that you can &#8230; <a href="http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/">Continue reading <span class="meta-nav">&#8594;</span></a>


Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/' rel='bookmark' title='Get email updates when your IP changes/ Python DynDns update client'>Get email updates when your IP changes/ Python DynDns update client</a></li>
<li><a href='http://www.kunaldua.com/blog/2005/04/the-internals/' rel='bookmark' title='The internals'>The internals</a></li>
<li><a href='http://www.kunaldua.com/blog/2006/08/reset-system-keychain-password/' rel='bookmark' title='Reset system keychain password'>Reset system keychain password</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The old &#8220;curl&#8221; based method stopped working yesterday when Reliance got a new login page as well as a new backend. It seems Reliance is now also looking at Cookies during authentication. Here&#8217;s a little Python script that you can execute to automate the process.</p>
<p>If you don&#8217;t know what Python is, you better stick to browser based authentication :-)</p>
<p>Needless to say, you can schedule this script as a cron/ launchd job to run periodically and keep you logged in. That&#8217;s how I use it, which is why the script doesn&#8217;t output anything to prevent unnecessary log &#8220;pollution&#8221;.</p>
<p><strong>Login Script for Python 2.x</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># encoding: utf-8</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Reliance Login Script for Python 2.x v1.0
&nbsp;
Created by Kunal Dua on 2009-12-18
http://www.kunaldua.com/blog/?p=330
&nbsp;
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>, <span style="color: #dc143c;">urllib</span>, <span style="color: #dc143c;">cookielib</span>
&nbsp;
username = <span style="color: #483d8b;">'1111111111111111'</span> <span style="color: #808080; font-style: italic;">#replace the text within quotes with your username</span>
password = <span style="color: #483d8b;">'password'</span>	<span style="color: #808080; font-style: italic;">#replace the text within quotes with your password</span>
&nbsp;
jar = <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">FileCookieJar</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cookies&quot;</span><span style="color: black;">&#41;</span>
opener = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib2</span>.<span style="color: black;">HTTPCookieProcessor</span><span style="color: black;">&#40;</span>jar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
response = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://10.239.89.15/reliance/startportal_isg.do&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
login_data = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlencode</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'userId'</span> : username, <span style="color: #483d8b;">'password'</span> : password, <span style="color: #483d8b;">'action'</span> : <span style="color: #483d8b;">'doLoginSubmit'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
resp = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://10.239.89.15/reliance/login.do'</span>, login_data<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Update: <strong>Logout Script for Python 2.x</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># encoding: utf-8</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Reliance Logout Script v1.0
&nbsp;
Created by Kunal Dua on 2009-12-22
http://www.kunaldua.com/blog/?p=323
&nbsp;
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>, <span style="color: #dc143c;">cookielib</span>
&nbsp;
jar = <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">FileCookieJar</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cookies&quot;</span><span style="color: black;">&#41;</span>
opener = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib2</span>.<span style="color: black;">HTTPCookieProcessor</span><span style="color: black;">&#40;</span>jar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
response = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://10.239.89.15/reliance/login.do&quot;</span>, timeout=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
resp = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://10.239.89.15/reliance/logout.do'</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Update: <strong>Login Script for Python 3.x</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># encoding: utf-8</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Reliance Login Script for Python 3.0 v1.0
&nbsp;
Created by Kunal Dua on 2009-12-30
http://www.kunaldua.com/blog/?p=323
&nbsp;
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>, http.<span style="color: black;">cookiejar</span>
&nbsp;
username = <span style="color: #483d8b;">'1111111111111111'</span> <span style="color: #808080; font-style: italic;">#replace the text within quotes with your username</span>
password = <span style="color: #483d8b;">'password'</span>	<span style="color: #808080; font-style: italic;">#replace the text within quotes with your password</span>
&nbsp;
jar = http.<span style="color: black;">cookiejar</span>.<span style="color: black;">FileCookieJar</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cookies&quot;</span><span style="color: black;">&#41;</span>
opener = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">request</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib</span>.<span style="color: black;">request</span>.<span style="color: black;">HTTPCookieProcessor</span><span style="color: black;">&#40;</span>jar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
response = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://10.239.89.15/reliance/startportal_isg.do&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
login_data = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">parse</span>.<span style="color: black;">urlencode</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'userId'</span> : username, <span style="color: #483d8b;">'password'</span> : password, <span style="color: #483d8b;">'action'</span> : <span style="color: #483d8b;">'doLoginSubmit'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
resp = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://10.239.89.15/reliance/login.do'</span>, login_data<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/' addthis:title='Reliance Wireless broadband auto-login (and logout) script(s) '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>

<p>Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2010/05/get-email-updates-when-your-ip-changes-python-dyndns-update-client/' rel='bookmark' title='Get email updates when your IP changes/ Python DynDns update client'>Get email updates when your IP changes/ Python DynDns update client</a></li>
<li><a href='http://www.kunaldua.com/blog/2005/04/the-internals/' rel='bookmark' title='The internals'>The internals</a></li>
<li><a href='http://www.kunaldua.com/blog/2006/08/reset-system-keychain-password/' rel='bookmark' title='Reset system keychain password'>Reset system keychain password</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
		</item>
		<item>
		<title>echo what you read with IFS</title>
		<link>http://www.kunaldua.com/blog/2009/02/echo-what-you-read-with-ifs/</link>
		<comments>http://www.kunaldua.com/blog/2009/02/echo-what-you-read-with-ifs/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 20:14:17 +0000</pubDate>
		<dc:creator>Kunal</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.kunaldua.com/blog/?p=193</guid>
		<description><![CDATA[Sample this code for reading a file line by line: 1 2 3 4 5 #!/bin/ksh while read LINE do echo &#34;$LINE&#34; done &#60; $FILENAME It works fine in most cases, where by most cases I mean your lines don&#8217;t &#8230; <a href="http://www.kunaldua.com/blog/2009/02/echo-what-you-read-with-ifs/">Continue reading <span class="meta-nav">&#8594;</span></a>


Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2005/04/the-internals/' rel='bookmark' title='The internals'>The internals</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Sample this code for reading a file line by line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/ksh</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> LINE
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$LINE</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #007800;">$FILENAME</span></pre></td></tr></table></div>

<p>It works fine in most cases, where by most cases I mean your lines don&#8217;t have any trailing spaces that you&#8217;d like to preserve. That&#8217;s right, read reserves the right to kill the trailing (and leading) spaces from your lines. Sample this (from <a href="http://nixdoc.net/man-pages/Linux/ksh.1.html">ksh man page</a>):</p>
<blockquote><p>
The IFS parameter specifies a list of characters which are used to break a string up into several words; any characters from the set space, tab and newline that appear inthe IFS characters are called IFS white space. Sequences of one or more IFS white space characters, in combination with zero or one non IFS white space characters delimit a field. As a special case, leading and trailing IFS white space is stripped (i.e., no leading ortrailing emptyfield is created by it); leading or trailing non-IFS white space does create an empty field.Example: if IFS is set to `<space>:&#8217;,the sequence of characters `<space>A<space>:<space><space>B::D&#8217; contains four fields: `A&#8217;, `B&#8217;, `&#8217; and `D&#8217;. Note that if the IFS parameter is set to the null string, no field splitting is done; if the parameter is unset, the default value of space, tab and newline is used.
</p></blockquote>
<p>So, the above code should be changed to this &#8211; just the one line added:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/ksh</span>
<span style="color: #007800;">IFS</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> LINE
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$LINE</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #007800;">$FILENAME</span></pre></td></tr></table></div>

<p>Now the above code gives the byte-by-byte output of the file. Or does it?</p>
<p>Problem is, if the file does not have an empty line at the end you&#8217;ll see that the last line never shows up in the output! So how do we fix this? The &#8220;old-fashioned&#8221; way &#8211; by adding an extra &#8220;echo $LINE&#8221; outside the loop.</p>
<p>Does it work fine now? Not quite. Now you&#8217;ll see an extra empty line at the end of the output file. How do we fix that? Use  &#8220;echo -n&#8221;. The &#8220;-n&#8221; option tells echo not to print the trailing newline character.</p>
<p>So finally we have our code to read a file line by line, do something with that line and write a modified line &#8211; and preserve every single bit along the way. Here it is then:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/ksh</span>
<span style="color: #007800;">IFS</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #660033;">-r</span> LINE
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$LINE</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #007800;">$FILENAME</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #007800;">$LINE</span></pre></td></tr></table></div>

<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.kunaldua.com/blog/2009/02/echo-what-you-read-with-ifs/' addthis:title='echo what you read with IFS '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>

<p>Related posts:<ol><li><a href='http://www.kunaldua.com/blog/2005/04/the-internals/' rel='bookmark' title='The internals'>The internals</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kunaldua.com/blog/2009/02/echo-what-you-read-with-ifs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

