2010-11-25

Perl one-liner: checking Listen ports on Apache HTTP configuration files.

You stumble upon an installation of Apache HTTP Server that has a few configuration files. You want to do a quick visual inspection of their Listen directives to check for conflicts.

This will also help you find if any Listens are duplicated within a single file. See port 443 in foo_httpd.conf.

jbm@Foucault:~/tmp/conf$ perl -mData::Dumper -ane '$f{$ARGV}->{$F[1]}++ if /^\s*Listen/; END { print Data::Dumper::Dumper(\%f);}' *conf
$VAR1 = {
'bar_httpd.conf' => {
'94' => 1,
'494' => 1
},
'admin.conf' => {
'8008' => 1
},
'blarf_httpd.conf' => {
'8011' => 1
},
'foo_httpd.conf' => {
'90' => 1,
'443' => 2,
'80' => 1,
'490' => 1
},
'httpd.conf' => {
'80' => 1
}
};

Yes, this is a long way to type a grep command, but I think the output lends itself more to visual inspection, which is what I'm after.

jbm@Foucault:~/tmp/conf$ egrep '^[[:space:]]*Listen' *conf
admin.conf:Listen 8008
bar_httpd.conf:Listen 494
bar_httpd.conf:Listen 94
blarf_httpd.conf:Listen 8011
foo_httpd.conf:Listen 90
foo_httpd.conf:Listen 443
foo_httpd.conf:Listen 80
foo_httpd.conf:Listen 490
foo_httpd.conf:Listen 443
httpd.conf:Listen 80

What Sony PIIQ clip-on earbuds (Qlasp) look like when worn.

Suppose you're in Target looking for new headphones for your iPod shuffle because your current behind-the-neck headphones get yanked out of your ears when you're doing push-ups and a few other exercises during your workout. You see what looks like a solution to your problem in the form of the Sony PIIQ™ clip-on earbuds (Qlasp™).

However, because the design is so unique, you are in doubt about how to actually wear them, and you want to confirm your suspicions about the design of these things. So you pull out your iPhone/Android and Google™ for images of folks wearing them but are surprised to find no photos of people wearing them, not even on the Sony site.

Problem solved.

Oh, right! Do they solve my problem? Well, I've stopped using my behind-the-neck headphones and exclusively use the PIIQ clip-ons.

I have not done a scientific survey to see which earphones I mess with less while working out. However, I'm kind of fussy about that. A normal person would probably not fiddle as much as I.

In general, I'd say that behind-the-neck phones are better for straight-out running (maybe, see below). PIIQ clip-on earbuds are better for the quasi-cardio strength training workout that I do when I'm not running. However, even on my runs, I do 22 push-ups and 22 leg lifts every mile, so I still wear my PIIQ clip-ons for my typical run.

I think configuring the PIIQ clip-ons is key to their staying put during activity. During my last run, I used the smallest set of earbuds that came with the PIIQ, and I reset my left earbud only once during the 3 miles (and 66 push-ups, 44 leg lifts, 22 sit-ups). For reasons yet unknown, I could not setup my right ear like my left, so I reset that earbud a lot. There was some subtle positioning I seemed to be missing.

Oops! I did not mean to turn this into a product review. I merely wanted to let other people know what these earbuds looked like while being worn. Here is another view from a slightly different angle.

2010-11-19

Perl one-liner: getting sorted fingerprint list from keytool.

I need to determine which X.509 certificates in one Java keystore are missing from another. I do this by comparing fingerprints. Here is how I get a sorted list of fingerprints from one keystore.

me@mybox:/usr/ibm_java/jre/lib/security
$ keytool -list -keystore cacerts -storepass changeit >~/keytool.out
me@mybox:/usr/ibm_java/jre/lib/security
$ cd ~
me@mybox:~
$ perl -ane '$f{$F[-1]}++ if /fingerprint/; END {for $k (sort {$a <=> $b} keys %f) {print "$k\n";}}' <keytool.out >fingerprints.list

I do this to both keystores.

Once I've got my list of fingerprints from each file, I get a list of fingerprints that are in one file, but not another.


me@mybox:~
$ diff fingerprints_123.list fingerprints.list | perl -ane 'print "$F[-1]\n" if /^>/' >diff_fingerprints.list

Then I wrote this script to read the list of missing fingerprints and tell me the aliases that correspond to these fingerprints in the keystore that has them.


#!/usr/bin/perl -w

# 1st arg = file with list of missing fingerprints.
# 2nd arg = file with keytool output from keystore with additional certs.

use strict;
use warnings;
use Data::Dumper;

# Slurp up all the missing fingerprints.
open my $want_fh, '<', $ARGV[0] or die;
my @wanted = <$want_fh>;
chomp @wanted;
close $want_fh;

my $previous; # Previous line of keytool.out.
my %data; # Key = fingerprint, value = line prior to fingerprint.

# Populate %data with all relevant keytool output from file with additional certs.
open my $data_fh, '<', $ARGV[1] or die;
while (<$data_fh>) {
chomp;
if (/fingerprint/) {
$data{(split(/\s/, $_))[-1]} = $previous;
}
$previous = $_;
}
close $want_fh;

#print Dumper(\%data);

# Find hash key in %data for each item in @wanted, and print the previous keytool output line.
for my $finger (@wanted) {
print "$data{$finger}\n";
}

exit 0;

Now that I think about it, I should have just written this as one bigger script instead of two one-liners and a script plus miscellaneous temp files. Oh, well. I'll do that another day.

2010-11-06

Perl one-liner: which directories do I need to restore?

I just ran an automatic process on a directory much higher up in the file system hierarchy than I wanted. Specifically, I accidentally did a Subversion Import on /Users from Xcode SCM dialog.

I can't just do a blanket restore starting at that top level directory. Specifically, Time Machine won't let me restore /Users. I quit Xcode shortly into the operation, and I need to know which subdirectories got hit so I can restore those.

How to do this? Do a find on which files were modified, then parse the output.

jbm@Foucault:~
$ sudo find /Users -mmin -15 -ls >/tmp/screwup.list 2>&1

jbm@Foucault:~
$ perl -ane '$p{join(" ", (split("/", $F[10]))[1..4])}++; END {for $i (keys %p) {print "$i\n";}}' /tmp/screwup.list 
Users jbm Library Preferences
Users jbm Library Application
Users jbm Code CoreDataTutorial
Users jbm Code MyApp
Users jbm Library Cookies
Users jbm Code Build
Users jbm Code SVN
Users jbm Library Caches
Users jbm Library Safari
Users jbm Code
Users jbm .DS_Store