Fun with Google geolocation

posted 2010-08-11 23:23:55, link to this article

After seeing Samy Kamkars talk at DEFCON 18, I really wanted to play with Google's geolocation API.
Some results already:

hessch@turing:~$ bin/mac_geoloc 00:11:92:a1:b2:40

{
    "location"    :    {
        "latitude"    :    36.1344023,  
        "longitude"    :    -115.1620542,
        "address"    :    {     
            "country"    :    "United States",  
            "country_code"    :    "US",
            "region"    :    "Nevada",  
            "county"    :    "Clark",   
            "city"        :    "Las Vegas",     
            "street"    :    "Las Vegas Blvd S",
            "street_number"    :    "2955",     
            "postal_code"    :    "89109"       
        },      
        "accuracy"    :    150.0}       
}

Which is the location one of the hotspot access-points in the Riviera hotel, according to Google. Yay! :)

Bash function to canonicalize MAC addresses

posted 2010-08-11 19:36:02, link to this article

I was playing with some network related stuff this evening and needed to rewrite MAC addresses to the canonical form. That is, something like this 00-02-DE-AD-BE-EF.
Why, you ask? Well every piece of software seems to have their own ideas on how to represent these magical 48 bit integers.
For instance, you'll often see something like 01:02:03:C0:FF:EE, which might also be written in shorthand as 1:2:3:c0:ff:ee or as a (pretty Cisco specific) perversion like 0102.03c0:ffee.
Indeed, a lot of variation...which makes parsing, a bit hellish.

Well I had written a pretty elegant parser in Perl before, that thing groks almost anything that you can throw at it.
But today I wanted to implement this in hesschlib, hesschlib is my private library of practical Bash functions. One of the rules of hesschlib is that things should be done in Bash whenever possible.
So I kludged up a small Bash function that does a pretty good job in rewriting most forms of MAC addresses that came to mind to the canonical form. Here it is:

# canonicalize MAC addresses (does grok most forms)
function maccanon {
    canon=''; for octet in ${1//[\.:-]/ }; do 
        [ ${#octet} -eq 4 ] && octet="${octet:0:2} ${octet:2:2}" ; 
        [ ${#octet} -eq 1 ] && octet="0${octet}"; canon=${canon}${canon:+ }${octet}; 
    done; echo ${canon// /-}; 
}

Which is a one-liner in hesschlib, of course, but broken up a bit here to make it more legible.
And finally, here's an example of maccanon in action:

    hessch@turing:~$ for mac in 00:02:DE:AD:BE:EF 1:2:3:c0:ff:ee 0102.03c0:ffee
    > do
    > maccanon ${mac}
    > done

    00-02-DE-AD-BE-EF
    01-02-03-c0-ff-ee
    01-02-03-c0-ff-ee

fi.sh - a Boids clone in Bash

posted 2010-02-22 19:29:02, link to this article
Read full article

After squa.sh, I thought it was time for another abuse of the Bash shell.
A few weeks ago I wrote fi.sh, a simulation of flocking behaviour, like migrating birds or a school of fish, written in pure Bash and with ascii-art graphics, of course.

Fi.sh is inspired by the classic Boids algorithm by Craig Reynolds. But since I wrote this in a week when I was in the middle of nowhere in Andalucia, without any means of connecting to the Internet it only uses two behaviours, instead of Reynolds’ three, still the results look pretty convincing.

Reynolds’ original Boids
Craig Reynolds original Boids

speed up debugging with viline

posted 2009-12-03 15:11:20, link to this article

This is a practical hack to speed up your coding-debugging cycles:
Most programming languages tell you that some error occurred in some file on line 666 like so:
Blah, blah. Some error in /some/file.foo:666. Blah.

Viline is a simple bash function that lets you copy and paste the file:linenumber combo from error messages as an argument to vi.
Vi will then start with the cursor at the offending line. Handy, eh? :-)

To use viline, add the following to your .bashrc:

    # viline: start vi with file:line
    function f_viline {  vim $(sed -r 's/:([0-9]+)$/ +\1/'<<<$1); }
    alias viline=f_viline
    alias vi=f_viline

The alias for vi itself is optional, myself, I never give vi any arguments but filenames, so this is pretty safe for me.
Otherwise just type viline instead of vi when you want to edit a file with a file:line style argument.

Progress bars in Bash

posted 2009-08-10 07:20:31, link to this article

Today a colleague reminded me to dig up a script that I wrote some years ago to generate progress bars (or actually generic horizontal bar graphs) in Bash.

The script is, in this state, more like a proof of concept. It displays an bar that increments until full and then decrements again, this cycle is repeated forever. It should not be too hard to rewrite it to be used in something more useful.
I think it's also trivial to rewrite this to display vertical bar graphs as well. I might write a small Bash library to include in other scripts later, if and when I feel the need to. ;-)

The code of the script follows below, enjoy...

#!/bin/bash

# progress bar
# Hessel Schut, VPRO Automatisering, 2004-04-02

cols=$(tput cols)
bold=$(tput smso)
norm=$(tput rmso)

trap cleanup EXIT TERM INT

cleanup() {
        norm
        tput clear
        tput cnorm
        exit
}

hor_line() {
        for ((i=1; i < cols - 2; i++)); do
                echo -n '-'
        done
}

draw_frame() {
        tput cup 2 2; hor_line
        tput cup 3 1; echo -n '|'
        tput cup 3 ${cols}; echo -n '|'
        tput cup 4 2; hor_line
}

tput clear
tput civis
draw_frame

d=1; pos=2; plen=0
while [ == ]; do
        [ $d -eq 1 ] && face=${bold}
        tput cup 3 $pos
        echo -n ${face}' '
        face=${norm}
        sts='pos: '${pos}' dir: '${d}
        tput cup 1 1; echo -n ${norm}${sts}
        [ ${plen} -gt ${#sts} ] && for ((i=0; i < plen - ${#sts}; i++)); do
                echo -n ' '
        done
        plen=${#sts}
        ((pos += d))
        [ $pos -gt 2 ] && [ $pos -lt $((cols - 2 )) ] || ((d=-d))
done

New version of Squa.sh

posted 2009-04-20 22:57:28, link to this article

I've made some changes to squa.sh, my Bash script Pong clone.

The first version made extensivce use of tput(1) to update the terminal cursor position.
I compared the resulting control sequences sent to various terminal versions, and all seemed to be in the format:

    ^[${row};${column}H

Therefore this new version uses pre cooked control sequences instead of forking tput like mad. dd(1) is still spawned each iteration of the main loop to capture user input, read(1) smallest timeout is 1 second, so this is no option, or the game would be no much fun. ;)

Also terminal size detection is fixed for NetBSD now, which means that the new version of squa.sh runs on all hardware in my house right now!

You can grab a copy of the new version at http://isquared.nl/src/squa.sh-ng/squa.sh.

squa.sh

posted 2009-04-19 19:37:58, link to this article
Read full article

Squa.sh is a litte squash game in a Bash shell script that I wrote this evening.

Follow the read link for more information or you can download the complete script from here.

Cobalt status LCD, NetBSD version

posted 2008-11-09 00:18:41, link to this article

Here is a NetBSD/Cobalt version of my Cobalt Qube/Raq LCD status screen updater.
The Debian linux version is described in an earlier post on this page, here: Cobalt Qube status LCD system monitor.

This script works fine on NetBSD/Cobalt 3.0, I haven't tested it on other versions yet.

You can download the new NetBSD version at http://isquared.nl/src/lcdbanner-netbsd.sh

Bourne/ Bash Shell CGI Scripts

posted 2008-11-01 21:49:09, link to this article
Read full article

Usually I do my server-side scripting in Perl, but some time ago when writing CGI scripts for an embedded system I ran into memory and flash space contraints. Because the system used Busybox, I had the Bourne compatible ash shell available.

So I wrote a very small Bourne shell script to include in my CGI scripts, which I share here, maybe it is of use to someone.