Main

January 09, 2008

amazon-util - thin wrapper around Net::Amazon

amazon-util wraps the search() method of Net::Amazon, and templates the output. This allows book searches to quickly be turned into product links:

$ amazon-util mode books power 'title: Amazon Hacks' <a href=http://www.amazon.com/o/ASIN/0596005423…

And the results quickly included in HTML, especially with the help of pbcopy(1) on Mac OS X. Example product link: Amazon Hacks.

Technorati Tags:

November 24, 2007

ShUnit - shell code unit testing

2008-05-11 update: shUnit2 is a unit testing framework for shell scripts modeled after the JUnit framework. It is designed to make unit testing in shell as simple as possible. I have not had a chance to review it yet, though may serve shell unit testing needs better than ShUnit (detailed below) does.

ShUnit is a unit testing framework for Bourne shell code. It does not ameliorate problems with syntax errors, but will confirm that the code behaves as expected, to the depth that unit tests can be written. Scripts bereft of modular design will benefit the least, as discrete functions offer more testing opportunities. The say_something script, shown below, employs a say_something function. This function is called directly from the unit test script. Additional unit tests could also run the say_something script and parse the output. Without this modular design, the unit test would be restricted to running the script and inspecting the output.

#!/bin/sh # say_something - a testable script say_something() { echo "$1" } broken_say_something() { echo $$ } say_something "uh oh I ran"

#!/bin/sh # say_something.test - unit tests for say_something . ShUnit-1.3/shUnit # source here requires no modification of the script being tested . say_something TestSaySomething() { EXPECTED="this is a test" RESULT=`say_something "$EXPECTED"` test "${EXPECTED}" = "${RESULT}" shuAssert "Test say_something" $? } TestBrokenSaySomething() { EXPECTED="this is a test" RESULT=`broken_say_something "$EXPECTED"` test "${EXPECTED}" = "${RESULT}" shuAssert "Test broken say_something" $? } InitFunction() { shuRegTest TestSaySomething shuRegTest TestBrokenSaySomething } shuStart InitFunction

$ sh say_something.test uh oh I ran ****** say_something.test ****** 2 tests to run: Test 1: TestSaySomething . Test 2: TestBrokenSaySomething E "Test broken say_something" failed. 2 tests run. 1 test succeeded.

However, whether sourced or executed, the code tested will run, evidenced by the uh oh I ran output, above. This may require an “is this the test mode?” workaround in the script, or that all the functions be moved to a library file that both the actual and any unit test scripts read from (an excellent design idea).

After my admittedly brief investigation, shUnit requires more code per test than Test::More for Perl. However, the included shUnitPlus library contains additional convenience functions that could further simplify the shell code. As a final plus, ShUnit employs clean design and clever code, and therefore makes for an edifying study.

Technorati Tags:

July 09, 2007

1yr

And, to commemorate one year of blogging, six lines taken at random from /usr/share/dict/words.

fibromyomectomy polemize typotelegraph nonratifying molestation Celticist

February 15, 2007

parse-sudoers - reformat sudoers data

parse-sudoers does not follow the sudoers BNF, though will tidy up the data, especially where unsorted users reside in sprawling multiline blocks.

$ cat input User_Alias SOME_USERS = finell, dobbs, bialy, \ garib, cavaluzzi, kubica, \ deutschman, noth, standford, mcelmurry $ parse-sudoers input User_Alias SOME_USERS = bialy, cavaluzzi,⏎ deutschman, dobbs, finell, garib, kubica,⏎ mcelmurry, noth, standford

Technorati Tags:

February 06, 2007

kronsoon - generate crontab timestamp for near future run

kronsoon generates a crontab(5) compatible timestamp in the near future, optionally followed by a command, optionally preceded by a comment. Similar to what an at(1) job can do, though handy where a utility must run from cron(8) to replicate the state of another command normally run under cron.

Technorati Tags: ,

January 15, 2007

replay-log - resubmit syslog data back to syslog

Use replay-log to play back Unix syslogd(8) logs at the original, increased, or random speed. Logs sent to standard output. The logger(1) utility can resubmit these logs to syslogd(8), or the output can be used to test sec.pl rules. Examples:

  • Replay logs from /var/logl/messages:

    $ replay-log < /var/log/messages

  • Replay logs with three second random delay instead of actual delays present in the logs:

    $ < /var/log/messages replay-log -r 3

  • Speed up literal playback:

    $ replay-log -f 3600 < /var/log/messages

  • Send sshd logs to a named pipe, and read them with sec.pl:

    $ mkfifo logfile $ grep sshd < /var/log/messages \ | replay-log -f 1000 -o logfile & $ sec.pl --conf=sshd.conf --input=logfile

Technorati Tags: ,

January 06, 2007

mathu - performs mathematical operations on input data

Wrote mathu a while back, when could not find a good (free) command line tool to perform quick calculations, such as:

$ cat input 7 4 2 5 $ mathu sum < input 18 $ mathu basic < input count 4 max 7 mean 4.5 min 2 sdev 1.8 sum 18

An updated tool could perhaps use Statistics::Basic or other module on CPAN to handle the heavy math lifting.

Technorati Tags:

December 25, 2006

xpquery - apply XPath statements to XML or HTML data

Use xpquery to apply XPath statements to XML data. xpquery requires the XML::LibXML and XML::LibXML::XPathContext modules. Good for quick extraction of data, or to easily test XPath queries. Some HTML can also be parsed, via the -p HTML option. This relies on the parsing support in libxml2, and will fail on poorly formed HTML content.

Another option for scripts: use the XML::XPath module, which offers convenience methods for accessing query results. Other XML parsing options include XSH.

Technorati Tags:

Continue reading "xpquery - apply XPath statements to XML or HTML data" »

December 24, 2006

initidy - reformat .ini style preference files

Use initidy to reformat *.ini style files. Optionally uses the Text::Autoformat module to clean up any comments present in the file. For example, the untidy input data:

[example] key = value key=value else= other longerkey = sometext

Becomes:

$ initidy < example [example] else = other key = value key = value longerkey = sometext

Technorati Tags:

December 03, 2006

OpenSSL S/MIME

Wrote up new documentation on using the openssl smime command. This page includes mime-util to help remove MIME left behind by openssl smime -decrypt or openssl smime -verify, as well as test scripts to help encrypt and decrypt S/MIME data.

Technorati Tags:

November 09, 2006

Chess960 position generator

Wrote chess960open to generate Chess960 opening positions for White. Here’s one possibility:

Bishop Rook King Rook Knight Queen Knight Bishop

Technorati Tags:

October 14, 2006

crush-png - handy wrapper around pngcrush

The crush-png script wraps pngcrush, allowing multiple files to be processed with the options I prefer. For GIF, try SuperGIF to compress data. GIF will usually reduce smaller than any corresponding PNG image. On a related note, patent encumbrances recently cleared from GIF.

September 29, 2006

RSA data length limits

The length of a RSA signature varies in direct proportion to the RSA key size, not the amount of data encrypted. The Perl script below demonstrates the length of signatures for several RSA key sizes. Also, larger keys allow more data to be encrypted with RSA, minus overhead for various encoding and security measures. Large amounts of data should be encrypted using a symmetric cipher, and the private key for this cipher encrypted via RSA.

#!/usr/bin/perl -wl use strict; use Crypt::OpenSSL::Random; use Crypt::OpenSSL::RSA; Crypt::OpenSSL::Random::random_status() or die "single and thine image dies with thee\n"; my $string = 'foo'; KEYSIZE: for my $ks (qw{512 1024 2048}) { my $pk = Crypt::OpenSSL::RSA->generate_key($ks); my $sig = $pk->sign($string); print $ks, ' -> ', length $sig; } __DATA__ 512 -> 64 1024 -> 128 2048 -> 256

Technorati Tags: , ,

Continue reading "RSA data length limits" »

September 25, 2006

sgrax - xargs backwards

sgrax - converts argument list to standard input for a named command. Opposite of xargs(1). Handy on (rare) occasion. Okay, very rare.

#!/bin/sh if [ -z "$1" ]; then echo "Usage: `basename $0` command data for standard input ..." >&2 exit 1 fi COMMAND=$1 shift echo "$@" | $COMMAND

September 24, 2006

Path Parser and Permissions Previewer Utility for Unix

Use parsepath to report Unix directory paths, or check whether a user or group has permissions to access named files. Great to quickly check CGI permissions where some parent directory sets the wrong permissions.

$ parsepath % /Users/jmates d 1775 root:admin / d 1775 root:admin /Users d 0755 jmates:jmates /Users/jmates $ parsepath +w /var/tmp $ parsepath +w /etc/passwd ! unix-other +w fails: f 0644 root:wheel /etc/passwd

Script originally filed under my debugging Unix pages.

Technorati Tags: ,

September 08, 2006

rename - mangle filenames using Perl expressions

rename - my enhancement of the original rename script by Larry Wall. Adds preview and copy support, plus documentation with examples. Assumes working knowledge of Perl. Did I mention the preview support?

Warning! Certain vendors install a useless but conflicting rename command under /usr/bin.

Technorati Tags:

September 01, 2006

Key and Certificate Conversion

Use convert2der to convert TLS key and certificate files from PEM to DER format and back again.

$ convert2der *.prv *.crt $ convert2der --inform=DER --outform=PEM *.prv *.crt

Great when a vendor tool only supports the DER format, but other tools and vendors generate PEM by default. Convenient wrapper around the openssl rsa(1) and x509(1) subcommands.

Technorati Tags: