Friday, April 20, 2007

A Few Useful Scripts

I have a set of perl scripts that I use fairly often. They convert numbers between decimal and hexadecimal, as well as to and from dotted-decimal. All are released under the GNU General Public License.

The calling syntax is the same for all of them:

$ dectohex 12345
or
$ echo 12345 | dectohex -
This allows them to be chained together.

[dectohex]

#!/usr/bin/perl
#
# Copyright (c) 2007  Michael A. Marsh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# The GNU General Public License is available at
# http://www.gnu.org/licenses/gpl.txt or by writing to the
# Free Software Foundation, Inc.
# 51 Franklin St, Fifth Floor
# Boston, MA  02110-1301 USA
#

$usage = "Usage: dectohex  | -\n";

$arg = shift @ARGV || die $usage;

sub do_translate
{
   my ( $dec ) = @_;

   $hex = sprintf "%X", $dec;

   print "$hex\n";
}

if ( $arg eq '-' )
{
   while(<>)
   {
      do_translate($_);
   }
}
else
{
   do_translate($arg);
}

[hextodec]

#!/usr/bin/perl
#
# Copyright (c) 2007  Michael A. Marsh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# The GNU General Public License is available at
# http://www.gnu.org/licenses/gpl.txt or by writing to the
# Free Software Foundation, Inc.
# 51 Franklin St, Fifth Floor
# Boston, MA  02110-1301 USA
#

$usage = "Usage: hextodec  | -\n";

$arg = shift @ARGV || die $usage;

sub do_translate
{
   my ( $hextotal ) = @_;

   $dec = hex($hextotal);

   print "$dec\n";
}

if ( $arg eq '-' )
{
   while(<>)
   {
      do_translate($_);
   }
}
else
{
   do_translate($arg);
}

[dectoip]

#!/usr/bin/perl
#
# Copyright (c) 2007  Michael A. Marsh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# The GNU General Public License is available at
# http://www.gnu.org/licenses/gpl.txt or by writing to the
# Free Software Foundation, Inc.
# 51 Franklin St, Fifth Floor
# Boston, MA  02110-1301 USA
#

$usage = "Usage: dectoip  | -\n";

die $usage unless scalar(@ARGV);
$arg = shift @ARGV;

sub do_translate
{
   my ( $dec ) = @_;

   die $usage unless $dec =~ /^\d+$/;

   $hextotal = sprintf "%x",$dec;

   @digits = split(//,$hextotal);

   while(@digits)
   {
      $digit = pop(@digits);
      $digit = pop(@digits) . $digit if(@digits);
      unshift(@pieces,hex($digit));
   }
   while ( scalar(@pieces) < 4 )
   {
      unshift(@pieces,0);
   }

   $ipaddr = join('.',@pieces);

   print "$ipaddr\n";
}

if ( $arg eq "-" )
{
   while(<>)
   {
      do_translate($_);
   }
}
else
{
   do_translate($arg);
}

[iptodec]

#!/usr/bin/perl
#
# Copyright (c) 2007  Michael A. Marsh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# The GNU General Public License is available at
# http://www.gnu.org/licenses/gpl.txt or by writing to the
# Free Software Foundation, Inc.
# 51 Franklin St, Fifth Floor
# Boston, MA  02110-1301 USA
#

$usage = "Usage: iptodec  | -\n";

$arg = shift @ARGV || die $usage;

sub do_translate
{
   my ( $ipaddr ) = @_;

   @pieces = split(/\./,$ipaddr);

   die $usage unless ( @pieces == 4 );

   for($i=0;$i<@pieces;++$i)
   {
      die "IP address must be in dotted-decimal form\n"
         unless $pieces[$i]=~/^\d+$/;
      $hexvals[$i] = sprintf "%02x",$pieces[$i];
   }

   #$hextotal = @hexvals[0..4];

   $hextotal = join '',@hexvals;

   $dec = hex($hextotal);

   print "$dec\n";
}

if ( $arg eq "-" )
{
   while(<>)
   {
      do_translate($_);
   }
}
else
{
   do_translate($arg);
}

Wednesday, April 18, 2007

rms reminisce

In keeping with my policy of violating my policy on not posting personal stuff, I had dinner this evening with Richard Stallman. It wasn't, of course, a one-on-one dinner. He was speaking at the university, and I was one of the group that went with him to dinner.

I have to say, I've read and heard a lot about his personality, and dinner was very pleasant and non-contentious. I was prepared for impassioned arguments throughout the meal. Granted, he spent much of the time catching up on work, but he'd certainly chime in with "GNU plus Linux" whenever someone slipped up and referred to "Linux." We were also watching ourselves to not accidentally say "open source" but rather "free software." I understand and sympathize with both points — he's right on both counts, if you want to be pedantic. As an activist who's always "on," it's his job to be pedantic, which can't be easy.

There wasn't much getting to know the guy, but I suspect he appreciated the opportunity to get some work done as the rest of us chatted, and he contributed significantly more to the conversation than just correcting our terminology. He seemed to enjoy his sushi, as well, certainly enough to get a couple of extra pieces.

What's the point of this post? None, really, other than to say, "Hey, I met Richard Stallman," and that, as someone who isn't a confidant, I found him reasonably pleasant to be around. Given many of the accounts I've read online, that seems to bear mention. You know, balance and all that.

Sunday, April 08, 2007

Impressions of an Idiot

Today, I mowed my lawn. From my last post, you know this means I pushed a reel mower around my yard. I also had to pick up a bunch of twigs, which took some time.

All told, I spent about an hour clearing my yard and mowing. I could tell I was getting a workout, and by the end I was ready to take off my jacket, even though it's in the 30s here.

My impression? It wasn't too bad. Given the first mow of the season is supposed to be the most difficult, since you're mowing older growth, I don't expect it to be a difficult lawn to mow. Negotiating the roots in the back was perhaps the most difficult part, but as I was mowing there I noticed that the grass wasn't particularly full, so some sort of ground cover would probably grow better there anyway.

The biggest surprise was that the slopes in my yard were much easier to mow than I'd expected. It helps that they're both short and narrow. Overall, I think my yard looks nice after the push mow, and I'll be guaranteed at least half an hour of cardio exercise every week during the growing season.

Thursday, April 05, 2007

Idiot Not Included

In another of an infrequent series of posts relating to me personally, as a new homeowner I suddenly have to worry about things of which I was blissfully oblivious in the past. Water in my basement was one such thing. Another is suddenly finding myself with a lawn that has decided, against all reason, to grow.

As a consequence of this lawn growth, I've bought a lawn mower. It has what my Dad refers to as a 1IP engine. That's one idiot-power. Yes, it's a push reel mower. This wasn't an effort to be cheap. Nor was it particularly an effort to be "green." Gas mowers are a pain, and electric mowers seem to suck. Electric mowers either have power cords, which, face it, sucks, or they have heavy batteries that make them a pain to push and that take a day to recharge (also sucks). I've used a good electric mower, but that model hasn't been made in years.

So, I researched my mower options, and a push reel mower seemed to make sense. For one, I have a very small yard. The front is mostly level, with slopes along the sides towards the back. The back is again mostly level, though with a fair number of tree roots. It's also only about half grass, with extensive slate work and some planter boxes. If the roots give me trouble, the back might become even less grass-covered.

If you're curious, I bought a Brill Luxus 38. I've only just assembled it and taken it for a test swath. I have to say, it's pretty nice. At $210, with free shipping, it's on the pricey end of push mowers, but it allegedly won't need sharpening for a decade. I've got it set at 4cm (it's made in Germany), which was a reasonably easy cut through some appreciable grass. So, while it's still a bit early to tell, I think I'm going to be happy with it. The weirdest thing about was that the assembly instructions were effectively in reverse. I suspect it would have been a lot easier to assemble the handle completely (one Phillips-head screwdriver required) before attaching anything to the reel. When mowing, it makes a pleasant shwuff shwuff shwuff sound — much nicer than the buzz of a gas or electric mower.