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

Parser/ Importer for the IEEE OUI list

posted 2010-08-07 01:14:19, link to this article
Read full article

In a distant past I created this vendor lookoup page for MAC addresses, that according to my Google Analytics is still being used from all over the world. Wow.
Most of the information in this tool was pretty outdated, because I had imported the IEEE oui.txt file in a SQLite database by hand and was too lazy to do that again.
Well, today I found some old Perl code back that I started on to automate this process. I never managed to get that to work in the past, somehow it worked after only a few edits today.
I quickly added some SQLite Perl DBI stuff, and suddenly the database for mac2vendor is up to date again.

I have published my code to download, parse and import the IEEE OUI file here or follow the read link for the full article.

From now on this script will run as a cronjob from time to time to keep mac2vendor more up to date.

Online MAC Address Vendor lookup

posted 2008-11-23 01:59:42, link to this article
Read full article

I have made an online version of my MAC address vendor lookup script. Of course there are many of these already, but well, choice is good, right? You can query my vendor lookup tool here: http://isquared.nl/doapp.html?appid=mac2vendor

MAC address vendor lookup

posted 2008-11-01 22:19:12, link to this article
Read full article

There are a couple of webpages where you can lookup the manufacturer of a piece of network equipment based on the OUI part of its MAC-address. That's neat, but not really if the device that you're trying to identify is, for instance, the rogue DHCP-server that knocked you off the Internet. And apart from that, I rather do as much as I can from within the shell instead of mousing to some website. That is why I've written a small shell-script that does the work for you.