Category Archives: code

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.

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 %sn" % (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!

Reliance Wireless broadband auto-login (and logout) script(s)

The old “curl” 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’s a little Python script that you can execute to automate the process.

If you don’t know what Python is, you better stick to browser based authentication :-)

Needless to say, you can schedule this script as a cron/ launchd job to run periodically and keep you logged in. That’s how I use it, which is why the script doesn’t output anything to prevent unnecessary log “pollution”.

Login Script for Python 2.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
# encoding: utf-8
"""
Reliance Login Script for Python 2.x v1.0
 
Created by Kunal Dua on 2009-12-18
http://www.kunaldua.com/blog/?p=330
 
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
"""
 
import urllib2, urllib, cookielib
 
username = '1111111111111111' #replace the text within quotes with your username
password = 'password'	#replace the text within quotes with your password
 
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
 
response = opener.open("http://10.239.89.15/reliance/startportal_isg.do")
 
login_data = urllib.urlencode({'userId' : username, 'password' : password, 'action' : 'doLoginSubmit'})
resp = opener.open('http://10.239.89.15/reliance/login.do', login_data)

Update: Logout Script for Python 2.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# encoding: utf-8
"""
Reliance Logout Script v1.0
 
Created by Kunal Dua on 2009-12-22
http://www.kunaldua.com/blog/?p=323
 
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
"""
 
import urllib2, cookielib
 
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
 
response = opener.open("http://10.239.89.15/reliance/login.do", timeout=2)
 
resp = opener.open('http://10.239.89.15/reliance/logout.do')

Update: Login Script for Python 3.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
# encoding: utf-8
"""
Reliance Login Script for Python 3.0 v1.0
 
Created by Kunal Dua on 2009-12-30
http://www.kunaldua.com/blog/?p=323
 
This program is free software; you may redistribute it and/or
modify it under the same terms as Python itself.
"""
 
import urllib, http.cookiejar
 
username = '1111111111111111' #replace the text within quotes with your username
password = 'password'	#replace the text within quotes with your password
 
jar = http.cookiejar.FileCookieJar("cookies")
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
 
response = opener.open("http://10.239.89.15/reliance/startportal_isg.do")
 
login_data = urllib.parse.urlencode({'userId' : username, 'password' : password, 'action' : 'doLoginSubmit'})
resp = opener.open('http://10.239.89.15/reliance/login.do', login_data)

echo what you read with IFS

Sample this code for reading a file line by line:

1
2
3
4
5
#!/bin/ksh
while read LINE
do
    echo "$LINE"
done < $FILENAME

It works fine in most cases, where by most cases I mean your lines don’t have any trailing spaces that you’d like to preserve. That’s right, read reserves the right to kill the trailing (and leading) spaces from your lines. Sample this (from ksh man page):

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 `:’,the sequence of characters `A:B::D’ contains four fields: `A’, `B’, `’ and `D’. 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.

So, the above code should be changed to this – just the one line added:

1
2
3
4
5
6
#!/bin/ksh
IFS=""
while read LINE
do
    echo "$LINE"
done < $FILENAME

Now the above code gives the byte-by-byte output of the file. Or does it?

Problem is, if the file does not have an empty line at the end you’ll see that the last line never shows up in the output! So how do we fix this? The “old-fashioned” way – by adding an extra “echo $LINE” outside the loop.

Does it work fine now? Not quite. Now you’ll see an extra empty line at the end of the output file. How do we fix that? Use “echo -n”. The “-n” option tells echo not to print the trailing newline character.

So finally we have our code to read a file line by line, do something with that line and write a modified line – and preserve every single bit along the way. Here it is then:

1
2
3
4
5
6
7
#!/bin/ksh
IFS=""
while read -r LINE
do
    echo "$LINE"
done < $FILENAME
echo -n $LINE

DigitalColor Meter

I was trying to “blend” a widget into the sidebar of this blog. No problem, I thought, I’ll go to the wonderful Element Inspector, and find out the current background color. I got the RGB value from the Inspector, converted it to Hex using exColor and sampled it, but the resultant color wasn’t the sidebar background color at all. The value matched the one specified in the CSS file for the sidebar alright, but the result on-screen was quite different from how it shows up in the blog! Intriguing.

Obviously, there are some intricacies of CSS at work there which I don’t quite comprehend at this point (the behaviour is consistent across all browsers). I promise to dig into that later and post my findings around here. However, the show had to go on – I needed to find out the color being actually displayed on screen and use it! So I set out trying to find a utility that could identify the color of any pixel on screen. On a whim, I typed “color” in QuickSilver and stumbled into /Applications/Utilities/DigitalColor Meter.app.

It does just what I said – tell you the exact value of any pixel on screen in RGB (absolute, percentage or hex) as well as other models. Have it running in the background and move your mouse to the pixel of your choice in any application. Just switch the focus back to DigitalColor Meter and you should see the color value corresponding to the pixel under the mouse pointer in the application last in focus. It has keyboard shortcuts to “hold” the current color value or to “lock” the focus of the mouse to the current pixel position, as well as copying the current color value to the clipboard – explore the menus, it’s quite simple and intuitive, really.

A word of warning though – do NOT keep it running in the background unnecessarily, not without putting it on hold at the least. Since it keeps updating itself as you move your mouse around and/ or switch applications, the whole process can take a toll on your CPU usage if done continously for long periods of time.

Another great utility that comes free, pre-installed on your Mac!

PS – For the record, ths sidebar color specifed is #666666. What you see is #ECFDCE.

The internals

Carrying forward from yesterday, let’s take a behind-the-scenes peek at the “Now Playing in iTunes” album art feature you see in the sidebar.

Here’s what you’ll need: 500 gms Mac OS X (sorry Windows people), 50 gms AppleScript, 2 tbspn Shell Script, 1 tbspn FTP, 1 Amazon developer ID, 250 gms PHP and 1/2 tbsp Cron skills.

Here’s how it functions. We schedule a Continue reading

Now playing!

Long time no see!

I don’t know about you, but I saw so many pranks flying around on April Fools’ Day, that I couldn’t be sure what’s for real and what’s not. For a moment, I was dead sure April was going to announce Tiger on the 1st, which happens to be their (Apple’s) anniversary I’m told, unless of course that was a smart ass’s idea of a joke.

Another reason for not much activity around here is Continue reading