Archive Page 2

Baudot fun

This is a throwaway piece of code, so it’s probably buggy but what the heck!

# -*- coding: utf-8 -*-
import sys

zero = '○'
one = '●'

numbers = []

def int2bin(n, count=5):
    """returns the binary of integer n, using count number of digits"""
    return "".join([ zero if ((n >> y) & 1) == 0 else one for y in range(count-1, -1, -1)])

def write(code):
    print numbers

def output(newstate, curstate, code):

    figs = 27
    ltrs = 31

    if newstate == 0 and curstate == 1:
    elif newstate == 1 and curstate == 0:


    return newstate

def translate(input):
    global numbers

    numbers = map(int2bin,range(0,32))
    baudot_letters = '@E@A SIU@DRJNFCKTZLWHYPQOBG@MXV@'
    baudot_symbols = '@3@- @87@$4\',!:(5")2@6019?&@./;@'

    cr = 8
    lf = 2
    sp = 4
    state = 0

    for c in input.upper():
        if c == '@':
        if c == ' ':
        elif c == '\n':
            ix = baudot_letters.find(c)
            if ix != -1:
                state = output(0, state, ix)
                ix = baudot_symbols.find(c)
                if ix != -1:
                    state = output(1, state, ix)

if __name__ == '__main__':
    for arg in sys.argv[1:]:

In case you're wondering, the Baudot code was invented in 1870 by Émile Baudot and later became the foundation of the international telex alphabets. See

The original telex machines were electro-mechanical beasts which used this 5-bit code for wire transmission. Telex machines could encode text to punched tape for later sending. They could also print incoming telex transmissions to tape as well as paper. I remember seeing in a post bureau in Pakistan a journalist receiving a message on one telex machine and feeding the output spool of tape into another as it arrived. An early version of message forwarding!

Example output of the above:

johncc@liberator:~$ python 'Hello world!'


cal – A command line calendar

If you already know about cal then you can move along, but if not please allow me to share this hidden gem with you.

(I find that the *nix command line is peppered with commands like this which are amazingly useful, and if you come from a *nix background they will be second nature to you. If, like me, you are a refugee from another OS, you may not have found some of them yet.)

With cal, it’s easier to show what it does than to explain it.

[~] $ cal
      May 2010      
Mo Tu We Th Fr Sa Su
                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

cal by itself gives you a calendar for the current month.

[~] $ cal -3
     April 2010             May 2010              June 2010     
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
          1  2  3  4                  1  2      1  2  3  4  5  6
 5  6  7  8  9 10 11   3  4  5  6  7  8  9   7  8  9 10 11 12 13
12 13 14 15 16 17 18  10 11 12 13 14 15 16  14 15 16 17 18 19 20
19 20 21 22 23 24 25  17 18 19 20 21 22 23  21 22 23 24 25 26 27
26 27 28 29 30        24 25 26 27 28 29 30  28 29 30            

cal -3 gives you a 3 month view.

What if you want a different month? No problem. cal mm yyyy will do it. Note that you need to use four digits for the year!

[~] $ cal 05 2009
      May 2009      
Mo Tu We Th Fr Sa Su
             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

Finally, cal -y will give you a year’s calendar. I won’t show an example here for reasons of space, but just try it for yourself. Naturally, you can do something like cal -y 2011 too.

There are a couple more options, so as always read man cal for the gory details.

Copy and paste across machines with ssh + xsel

This is pretty obvious with hindsight but I only just thought of it!
I often have my work laptop on the desk next to my home desktop, and sometimes for whatever reason I need to copy & paste a URL or similar snippet between the machines.

Firstly, if you’ve never used xsel before, it’s very useful. Pipe something into it, and it becomes the current selection. Use it without input and it outputs the current selection buffer. It has a few more options, so check the man page.


grep foo ~/bar | xsel
xsel < ~/.conkyrc
xsel > ~/saved

Secondly, I strongly recommend that you set up public key authentication and ssh-agent if you haven’t already. It will make your life much easier!

Now the important part. To transfer the paste buffer from the current machine to a remote machine:

xsel | ssh -X "DISPLAY=:0.0 xsel -i"

Obviously you can replace the input with a local file or the output of a command just as easily, e.g:

ssh -X "DISPLAY=:0.0 xsel -i" < foo.html

In case you aren’t familiar with the traditional X11 paste method, this means you select something on the source machine, run the command above, and then use the middle mouse button on the remote machine to paste. If you don’t have one, use the left and right buttons simultaneously.

Hope this is useful!

How to remove thumbnails from a directory containing images

Following up from my last post about grabbing images from a squid cache, I needed a better way to remove thumbnails than just looking at file sizes.

I decided to try and get the image dimensions and then look at the mean width/height value.

This was what I came up with:-


for file in *; do
    size=$(identify -format "%w %h" "$file")
    w=$(echo $size | cut -f 1 -d " ")
    h=$(echo $size | cut -f 2 -d " ")
    mean=$(( ($h + $w)/2 ))
    if (( mean < 300 )); then
        echo "$file $h $w $mean"
        rm "$file"

You will need the excellent ImageMagick package, which is available in most distros.

How to extract images from your squid cache

When I searched for this, I found a lot of questions but no real answers.  Based on a couple of hints I found online, I put these scripts together which might be of use to someone else.

First, the script to scan the cache for images we want to extract.


# -- Edit these as needed !
# --


set -u

# Limit to files above 15k size in order to skip thumbnails where possible
find "$cache" -size +15k -type f | while read file
    if head "$file" | grep -q "$match"; then

        content_type="$(head "$file" | grep -ai "Content-Type:" | cut -f 2 -d " " | tr -d '[:cntrl:]')"
        echo "Matched $file ... content type $content_type" >&2

        if [[ "$content_type" = 'image/jpeg' ]]; then
        elif [[ "$content_type" = 'image/png' ]]; then
        elif [[ "$content_type" = 'image/gif' ]]; then

        if [[ "$type" != "" ]]; then
            outfile="$output$(mktemp $filebase).$type"
            echo "Converting $file as $outfile with type $type" >&2
            squid_get_image -q -t $type $file $outfile < /dev/null


Save this as ~/bin/squid_scan or similar (if ~/bin is in your path) and do chmod 755 ~/bin/squid_scan

This uses some fairly rudimentary filtering, which could be improved. To use it to grab all images from, you call: squid_scan mysite

As you can see, this calls squid_get_image to actually do the image extraction. Here’s the code:-


set -u

    echo "squid_get_image [-q] [-t ] infile outfile" >&2
    echo >&2


while getopts ":qt:" OPTION
    case $OPTION in
        echo "Invalid option: -$OPTARG" >&2
        exit 1

shift $(($OPTIND - 1))

if [[ $# != 2 ]]; then
    echo "Expecting 2 parameters, got $#" >&2
    exit 1


$quiet || echo "Coverting $infile to $outfile" >&2
case $im_type in


    bvi -f <(echo -e '\FFD8\,$w' $outfile\\nq) "$infile" &>/dev/null


    bvi -f <(echo -e '\89504E470D0A1A0A\,$w' $outfile\\nq) "$infile" &>/dev/null


    bvi -f <(echo -e '/GIF8[79]a/,$w' $outfile\\nq) "$infile" &>/dev/null


    echo "Bad Image Type." &>2


NOTE: You need the bvi utility (binary vi). In Debian/Ubuntu just sudo apt-get bvi. In Archlinux it’s in aur.

Save this as ~/bin/squid_get_image or similar and do chmod 755 ~/bin/squid_get_image

This uses bvi’s ability to run a script of ex commands. It uses the fact that the squid cache files are some binary data, the HTTP headers, and then the file itself. The gif, jpg and png formats all start with “magic numbers”, and the ex commands look for those before writing out everything that follows to the specified file name.

I hope this is useful to someone!


I have to interact with a lot of Windows systems, so pptp is the norm.  I’ve often tried to configure IPSEC but to honest, I’ve never made it work!  I read up on it, understood it in theory and then sat down to try again.  But heterogenous systems (Linux, my router, their router, etc.) all seem to present a different set of incomprehensible options with different names.  In the case of the routers, add questionable documentation.

Anyway, I just discovered the networkmanager-pptp plugin.  First, I installed network-manager-pptp and pptp-linux.  You need to make sure that your wired interface is configured to roam, otherwise you will not see the options for VPN configuration.

After that, I configured my VPN but couldn’t work out how to make the option appear to connect to it.  After some Googling, I found the answer:-

sudo /etc/dbus-1/event.d/25NetworkManager restart
sudo /etc/dbus-1/event.d/26NetworkManagerDispatcher restart

I suppose a reboot would have done it but I like to avoid those!

Now, I can just click and connect instantly.  Very neat!

My desktop

Having switched from KDE to GNOME about 6 months ago, I got to like GNOME a lot but found some aspects of Metacity annoying.  I quite liked compiz-fusion but found that I tended to turn most effects off so was not really making good use of it.  I decided to try out openbox with GNOME and really liked it.

Following some of the great info online, I was seduced by the idea of trying a pure openbox session, and that is what I’m still running.

Why all the changes you might ask?  Well, having used Linux full-time for a year and a bit and having mainly stuck with the same bunch of things, I wanted to really take time to look around and see what was out there.  Also, it’s all about learning new (old?) ways of doing things, new skills, and getting a deeper understanding of the free software universe that I’m coming really to adore.

The other reason is that my feelings have changed about two aspects of Linux that scare new users sometimes, especially Windows refugees.  The first is that there are hundreds of choices to be had, and that is a good thing not a bad thing.  To those used to the idea that there is one OS, one Word Processes, and to whom the idea of mixing and matching panels, window managers, etc is alien, this can be vertigo-inducing.  But now, I’m like a kid in a sweet shop.  All these amazing things to try!  So much freedom to tweak and mix!  The second is that I am losing the need to have the comfort blanket of Windows-UI metaphors around.  KDE is great and I do like it, and I’m looking forward to KDE 4.1.  GNOME is great too – clean and simple and well-integrated.  But when I used to see things like Fluxbox, ICEWM, or apps like MUTT, vim, and others I used to run screaming thinking “ugly! dated! complicated!”  Now, I’m willing to try anything and find the appeal of these light, fast, customisable tools growing all the time,

So I’m currently running openbox, xcompmgr, nitrogen, wbar, pypanel, conky and gdesklets.  I’ve finally purged all KDE apps and libs from my system and am trying to live without my last few favourites (amarok, k3b, TaskJuggler, KTorrent, digikam).  I am using transset to modify window transparency with the mouse-wheel.

I’m really finding this combination awesome and a joy to work with at the moment.  That’s not to say I won’t start tweaking again soon!

Top artists this week from

My Twitter feed