2008-04-30

the machine that goes PING!

Meet ~bin/sonar.
#!/bin/sh
# usage: sonar <host or ip address>
while :
do
ping -c 1 $1 && say "PING!"
sleep 30
done
This is a super fancy network monitoring tool. The original version of this was from another friend named Mark (not he of greenbar fame) and it played a submarine style sonar "ping" sound when a ping was successful. When you're waiting for your server to come back up, and you have time for a nap until it does, a tool like this is just what you need.

This version relies on the Mac OS X /usr/bin/say command.

Eat your heart out peep.

EDIT: Yeah, as originally posted the ping command was missing an argument. I blame myself and blogger's difficult to use posting. Thanks, Aaron.

2008-04-21

Drop Anything

Sometimes you need drag and drop support for your scripts. Luckily, there's this Mac OS X tool which turns your scripts into applications onto which you can drop files and present their paths as arguments to your script. It's called DropScript and you can find it on Willfredo Sanchez's software page.

I used it to feed input to a remote python program and it works perfectly. Of course you need to give it a cute icon so that people will want to drop files on it.  Here's one I like:

DomocunCreature Icon

2008-04-18

This is getting awk-ward

Ideas from readers DrewP and Justin have greenbar approaching the size of the original awk version. Thank you both.

#!/usr/bin/python -u

import sys

try:
pat = sys.argv[1]
except IndexError:
pat = None

green = True
for line in iter(sys.stdin.readline, ""):
if (pat and pat in line) or (not pat and green):
print "\x1b[7m%s\x1b[0m" % line.rstrip()
else:
print line,
green = not green

2008-04-11

Please to dance round for the one called the Greenbar

Inspired by what I can only guess is my unsightly python version of greenbar, drewp has created a version with 100 percent more fun to look at. Thanks!

Drewp's version (see here) is what I always hoped greenbar would grow up to look like when it was written by someone who was not me. I wish the enumerate thing would work - I tried that originally too, but no, it's not to be. It works fine if the input is a regular file, but not when you want to read the stdout of another process. There's internal buffering in python that chunks up the input making the most common use of greenbar, watching a growing log file, hopeless.

Combining the two versions results in a super hybrid that works and is easy on your eyes. Tada!
#!/usr/bin/python -u

import sys

try:
pat = sys.argv[1]
except IndexError:
pat = None

lineno = 0
while 1:
line = sys.stdin.readline()
if not line:
break
if (pat and pat in line) or (not pat and lineno % 2 == 0):
print "\x1b[7m%s\x1b[0m" % line.rstrip()
else:
print line,
lineno = lineno + 1
P.s. The original awk version was shorter still. If I find a copy, I'll bury it so that python looks better.

2008-04-08

spice up your tail minus effs with greenbar

I've been missing this tool for a while now. The original "greenbar" was in written in awk, probably by my friend Mark S. It took input and inverted the text of every other line (like greenbar printer paper). It made watching log files scroll up your terminal much easier to parse.

Here's my python version which has one new feature. It can be given a string argument in which case it will only highlight lines in its input that contain that string.


#!/usr/bin/python -u

import sys

REG = "\x1b[0m"
INV = "\x1b[7m"

try:
pat = sys.argv[1]
except IndexError:
pat = None

i = 0
while 1:
line = sys.stdin.readline()
if not line:
break
if pat:
if pat in line:
print INV, line,
else:
print REG, line,
else:
if i % 2 == 0:
print INV, line,
else:
print REG, line,
i = i + 1
if i:
print REG

It's important to use unbuffered I/O, thus the "-u" flag to python is used along with sys.stdin.readline().