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
Why I love HTTP
posted 2010-04-25 13:01:16, link to this article
A few years ago, I worked at a company whose webservers were hosted at
a dedicated web-hosting company.
The question rose whether their website could be reached at foo.bar as well as at www.foo.bar.
Well, no problem you'd say, nothing that a simple A or CNAME record in DNS couldn't fix.
Not so, foo.bar had an A record assigned already for a host in the company's DMZ that used to do a *lot* of stuff.
You can imagine adding a webserver to foo.bar, just to serve 301 moved permanently status codes seemed like a bit of overkill to me.
And apart from that, I was too lazy to have another $many_lines_of_code to keep up to date for such a trivial task.
Thanks to the wonders of HTTP the solution was almost as simple as the problem.
I whipped up a small text file containing a static HTTP 301, and used the almighty inetd and cat to glue this to a socket.
It worked like a charm. It was also great to see sites like Netcraft list the webserver of foo.bar as cat/0.9. :-)
I thought of this today, when I was playing a bit with my Nintendo DS running DSLinux.
I could find various MP3 decoders, but nothing to do streaming. So I kludged together the other side of the hack that I explained above:
#!/bin/sh
(nc mp3.streampower.be 80 <<_GETREQ
GET /stubru-low.mp3 HTTP/1.0
_GETREQ
) | sed -n '/^$/,$p' |\
madplay -
Et voila. Streaming audio. (In this case the Belgian music station Studio Brussel.)
Thank you, sir Tim.
Sitecom WL-340 shell commands via HTTP
posted 2010-04-18 16:40:26, link to this article
My local Media Markt (a large German chain of electronics stores) had some cheap NAT routers for sale. Out of curiosity I bought one.
This router (the Sitecom WL-340) runs Linux and the internal console port is nicely labeled and that alone made me buy two more as you can never have too much hardware to play with. ;)
When poking around in these boxes I found that they have a nice hidden web page to run commands on these boxes.
You can access this page as /system_command.htm on your routers webinterface.
I thought it might be useful to access this command shell from scripts, so that you can use stock WL-340s for various "unintended" purposes. ;)
With the help of the amazing Firebug extension for Firefox I managed to use the remote command facility of these routers from the shell.
Like so:
hessch@kirchhoff:~$ curl -s -o- -u admin -d command='cat /proc/cpuinfo' \
-d next_file=system_command.htm -d todo=system_command \
http://192.168.11.128:8080/setup.cgi | \
sed -n '/outlog_Display/,/\/textarea/{/textarea/d;p}'
Enter host password for user 'admin':
system type : Ralink SoC
processor : 0
cpu model : MIPS 24K V4.12
BogoMIPS : 212.99
wait instruction : yes
microsecond timers : yes
tlb_entries : 32
extra interrupt vector : yes
hardware watchpoint : yes
ASEs implemented : mips16 dsp
VCED exceptions : not available
VCEI exceptions : not available
Now, this only needs a small wrapper shell script and it's ready for (ab)use.
I'll probably post some more hacks based on these boxes later on.
Fix for Thinkpad T400 WWAN on Ubuntu 9.10
posted 2010-02-25 19:51:08, link to this article
The builtin WWAN modem of Lenovo ThinkPad T400 laptops does not seem to wake properly when the laptop is suspended on Ubuntu.
I have written a small Power Manager script to fix this problem, it seems that Ubuntu 9.10 (Karmic Koala) does not support wakeup scripts in /etc/acpi/resume.d anymore.
Although it tires me a bit how much Ubuntu seems to break in new releases lately, it was worthwhile to search for the mechanism "du jour".
It seems that scripts need to be placed in /etc/pm/sleep.d nowadays.
Add the script below to a new file, /etc/pm/sleep.d/10_thinkpad_wwan, and it should be called upon suspend and resume to revive your modem on resume.
#!/bin/sh
# Action script to reinitialize built-in WWAN of Lenovo ThinkPads
# on resume
#
# Hessel Schut, hessel@isquared.nl, 2010-02-25
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin
case "${1}" in
hibernate)
# unload cdc_wdm module
rmmod cdc_wdm
;;
resume|thaw)
# revive RF and reload cdc_wdm module
echo 1 > /sys/devices/platform/thinkpad_acpi/rfkill/rfkill1/state
modprobe cdc_wdm
;;
esac
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.
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.
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.
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.
Cobalt Qube status LCD system monitor
posted 2008-11-01 21:58:29,
link to this article
Read full article
This is a handy framework to monitor several system parameters on Cobalt hardware running Debian GNU/Linux.
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.
isquared.nl rss (atom)