This is the website of Kunal Dua. He's a telecom billing consultant by day and a Mac nerd by night.

Eating jalebis, one bad driver at a time

While driving back home last evening I found myself constantly criticizing the driving sense (or lack thereof) of my fellow drivers. I blamed the drivers, the “mixed” traffic pattern, and the authorities for issuing driving license to one and all, for the mess that is our roads. With these thoughts in mind, I was almost home, when I took a left turn and found myself “face to face” with a sedan.

Now this guy was clearly in the wrong, but he didn’t even think twice before gesturing me to back up and let him go. I was in no mood to budge, not to mention I was right, and refused (at which point I may have used a certain finger to tell him to back up himself). After 5 minutes (kid you not!) of both of us sitting in our respective cars (did I mention I ate jalebis to show him I was in no hurry and would see this through?), he decides to get off and talk. He says that he uses this (wrong) route everyday while coming back from his office, so why don’t I just back off and let him go. I told him that I don’t care, you are in the wrong lane, and if anyone should be backing up, it’s you. Unimpressed, he goes back to his car.

We sit in our cars for another 5, maybe 10, minutes before he decides to back up and let me go – told you I wouldn’t budge. During that time, I had cycle-wallahs telling me “peeche kar lo na sir, kya jaayega”, another car lining behind the other car (again on the wrong side, needless to say), trying to honk his way out of trouble, realizing I won’t move, backing up, and taking another (wrong) route to escape our tamasha. I also tried calling cops, since I couldn’t find any around – I am still waiting for the response to my 100 call, Delhi Police!

In case you are wondering how our “shenanigans” didn’t create a traffic snarl, it’s because instead of taking the designated left turn, people kept going straight and taking the 90° left – it’s the “done thing” at that turn, perhaps the reason why our friend has made this his “regular route”.

Why did I do it? Let’s just say I had enough of people breaking traffic rules without giving it any thought. I wanted to teach him a lesson, though I am under no illusions that the lesson would last a lifetime, if he’s “learnt” anything at all – he probably thinks I was someone with too much free time on my hand, and that he was a “bigger person” for backing off. Would I do it again? Absolutely – unless I was in UP of course, where I would happily back off rather than risk getting shot!

Bookmark and Share

Bespin Gotchas

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(
   # set to true to allow connections from other machines
   address="0.0.0.0",
   port="8080",
   try_build=False,
   dburl=None,
   async=False,
   config_file=path("devconfig.py"),
   directory=path("../bespinserver/").abspath(),
   clientdir=path.getcwd()
),

Or pass it as a command line argument:

paver server.address=0.0.0.0 server.port=8080 start

In case you haven’t noticed, you can even specify the port to start on i.e. run Bespin on a custom port.

Bookmark and Share

Sorted, Human Readable file sizes via du

du -d 1 | sort -n | cut -f 2 | xargs du -h -d 0
Bookmark and Share

Talk about UI failure

So you won’t let me get to the content just because “there are no surveys available in my country”? Thanks Tinypaste!

tinypaste fail
Uploaded with plasq’s Skitch!
Bookmark and Share

Get email updates when your IP changes/ Python DynDns update client

I use DynDns to map my ISP provided dynamic IP to a static hostname. For some reason, the DynDns provided update clients don’t always work for me and often leave my hostname pointing to a dead or (worse) someone else’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.

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.

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’s the script, with an explanation afterwards.

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
#!/usr/bin/env python
# encoding: utf-8
"""
Script to email IP whenever it changes. Also updates DynDns hostname.
Version 1.0
 
Created by Kunal Dua on 2010-05-10
http://www.kunaldua.com/blog/?p=360
 
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
"""
 
def send_mail(subject, content):
        import smtplib
        from email.mime.text import MIMEText
        SERVER = "smtpserver"
        PORT = 587  #Use 25 if this doesn't work
        USER = "username"
        PASS = "password"
        FROM = "IPBot <mail@example.com>"
        TO = "user+folder@example.com"
 
        SUBJECT = subject
        TEXT = content
 
        message = MIMEText(TEXT)
        message['Subject'] = SUBJECT
        message['From'] = FROM
        message['To'] = TO
 
        server = smtplib.SMTP(SERVER, PORT)
        server.login (USER, PASS)
        server.sendmail(FROM, TO, message.as_string())
        server.quit()
 
 
def update_dyndns(theip):
        USERNAME = 'username'
        PASSWORD = 'password'
        HOSTNAME = 'example.dyndns.org'
 
        theurl = 'https://%s:%s@members.dyndns.org/nic/update?hostname=%s&myip=%s&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG' % (USERNAME, PASSWORD, HOSTNAME, theip)
 
        import urllib
        conn = urllib.urlopen(theurl)
        #print conn.read()
        conn.close()
 
 
if __name__ == '__main__':
        import urllib2, re
        conn = urllib2.urlopen('http://checkip.dyndns.com/')
        data = conn.read()
        conn.close()
        m = re.search('([0-9]*)(\.)([0-9]*)(\.)([0-9]*)(\.)([0-9]*)', data)
        currip = m.group(0)
 
        lastfile = "lastip.txt"
        allfile = "history.txt"
 
        theipfile = open(lastfile,"r")
        lastip = theipfile.read()
        theipfile.close()
 
        if lastip == currip:
                #print "no change needed"
                exit()
        else:
                histfile = open(allfile, "a")
                import datetime
                thenow = datetime.datetime.now().ctime()
                histfile.write("%s %s\n" % (thenow, currip))
                histfile.close()
                theipfile = open(lastfile,"w")
                theipfile.write(currip)
                theipfile.close()
                send_mail(currip, '')
                update_dyndns(currip)
  • Lines 17-22 and 39-41 replace with your email and DynDns settings respectively.
  • Line 22 – 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’s a useful trick to prevent these mails from cluttering up your inbox. If not, simply enter your regular email address.
  • 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 – because you can!
  • Line 78 – 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.
  • Note: 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.

Recommended frequency of running this job via cron/ launchd is 10 minutes.

Update: (May 27) I am pretty sure the DynDns API is broken in some way because I can’t get it to update even with this script. The email part is working pretty good for me though!

Bookmark and Share