The httpd.conf exploder!

Posted on November 30, 2006

I thought that it’d be easy to break up a httpd.conf file up into multiple files per VirtualHost using a single line of perl. Turns out, I was most mistaken. Here is the one loooong line of perl to perform the explosion.

perl -ne ' if (/<Virtualhost (\*:80|[0-9.:]+)>/ .. /< \/VirtualHost>/) { $data.=$_ }; if (/ServerName/) { chomp; $sn=$_; $sn=~s/\s*ServerName\s*([^\s]+)\s*/$1/; } if (/<\/VirtualHost>/) { open(outfile, “>>$sn.conf”); print outfile $data; close outfile; $data=”" } ‘ < httpd.conf

The Nunar Reaper

Posted on June 06, 2005

People keep hammering my sshd with false login requests. I wrote this script which I call nunar_reaper.pl that retaliates against the stupidity in favor of a tarpit. The infamous Dave Dellanave helped out with this one. I still need to fend off imaginary user name attacks, but that’s a little harder.

#!/usr/bin/perl
open(TAIL, "tail -f /var/log/secure|");
while() {
  if(/Failed password for root/) {
  ($ip) = $_ =~ /(\d+\.\d+\.\d+\.\d+)/;
   system "iptables -A INPUT -i eth0 -s $ip  -j DROP"
  }
}

PerlMagick+Garmin GPS Data+USGS Orthoimages

Posted on May 27, 2005

I finally got a day off from work, and managed to write a little perl script to take my garmin forerunner xml watch data and overlay it on a high-resolution USGS Orthoimage. USAPhotoMaps will not do this nor will GPS Visualizer. So, I basically had to roll my own script to do this chore. The CPAN Perl Module PerlMagick made short work of this. I had quite a bit of trouble producing an image this big on my computers. The actual TIFF data for the satellite image from USGS was nearly 500MB. The full resolution satellite image was 16000×10000 for the entire town lake trail (which I stupidly ran on Wednesday). ImageMagick crashes on handling an image this big even though my Linux machine has 1536MB (1.5 Gigabytes) of RAM. Adobe Photoshop fortunantely handled the full-res 16000×10000 image, and allowed me to resize it to a 9000×5648 TIFF. Then, my PerlMagick read the TIFF file in, and produced the GPS Track annotation and saved it. I then used command-line ImageMagick to mogrify the TIFF to a JPEG, which is what I added to my gallery. Enjoy.

Tony Versus Tony and Recap

Posted on September 14, 2004

I managed to lop-off the unnecessary space between -e and the expression, yielding a 24-character masterpiece.

    000000000111111111122222222223333
    123456789012345678901234567890123
%1: awk '{printf ""%s"\n",$1};'
%2: perl -ane ' print ""$F[0]"\n" '
%3: perl -pe 's/(\S*).*/"$1"/'
%4: perl -ape 's/.*/"$F[0]"/'
%5: perl -ape 's^.*^"$F[0]"^'
%6: perl -ape's#.*#"$F[0]"#'

Tony Versus Bumper

Posted on September 14, 2004

Not to be outdone, I immediately came back with a shorter version:

        00000000011111111122222222222
        12345678901234567890123456789
tony%   perl -ape 's/.*/"$F[0]"/'
bumper% perl -pe 's/(\S*).*/"$1"/'

OK, I managed to knock a character off by combining split and print mode. We’re now down to a 25-character perl solution.

Awk Versus Bumper

Posted on September 14, 2004

Bumper managed to one-up the awk code with this one.

00000000011111111122222222222
12345678901234567890123456789
perl -pe 's/(\S*).*/"$1"/'
awk '{printf ""%s"\n",$1};'

Perl put’s the smackdown on awk by three whole characters. Bumper noted that Perl’s binary is one character longer than awk. So, he thinks we claimed that we should some how normalize this by allowing them to be called as one character aliases. But, awk came before Perl, so the three-character binary name pool wasn’t nearly as exhausted as it is these days. So, I claim that it has to use standard invocation to be a winner.

Awk Versus Perl

Posted on September 14, 2004

I was trying to get awk to print the first column of a file and put double quotes around each item. I finally figured out how to do it after breaking out my underused Oreilly “Awk Programming” book. In case you wondered, awk is most efficient at displaying individual whitespace delimited columns from arbitrary files. The syntax is quite simple and easy to remember.

% seq 1 9 | xargs -n 3
1 2 3
4 5 6
7 8 9
% !! | awk ' { print $1 } '
1
4
7

The syntax for slapping double quotes around the colums is a little trickier:

% !-1 | awk ' { printf ""%s"\n", $1 }; '
"1"
"4"
"7"

Little did I know, perl can do the identical thing with the little-known -ane tag.

% !-2 | perl -ane ' print ""$F[0]"\n" '
"1"
"4"
"7"

So, what language requires less typing (unnecessary spaces removed)?

0000000001111111111222222222233
1234567890123456789012345678901
awk '{printf ""%s"\n",$1};'
perl -ane 'print ""$F[0]"\n"'

Awk narrowly edges-out Perl 29 to 31!!!!