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);
}

No comments: