shellspells beta

perl

perl [ -sTtuUWX ] [ -hv ] [ -V[:configvar] ]
     [ -cw ] [ -d[t][:debugger] ] [ -D[number/list] ]
     [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal/hexadecimal] ]
     [ -Idir ] [ -m[-]module ] [ -M[-]'module...' ] [ -f ] [ -C [number/list] ] [ -P ]
     [ -S ] [ -x[dir] ] [ -i[extension] ]
     [ [-e|-E] 'command' ] [ -- ] [ programfile ] [ argument ]...

perl is a text processing language.

Perl bridges the gap between a quick bash script and a fully fledged C program quite neatly. Personally, I don't find too many uses for it, but when I do, it does a good job.

Scripts

subnet

Subnet calculator - applies a netmask to a network and outputs the resulting subnets.

View code
#!/usr/bin/perl
# Applies a netmask to a network and outputs the resulting subnets

my $opt_range=0;

#--------------------------------------------------------------------------
sub pbin
{
  my @bstr;

  for ($i=1; $i<=8; $i++)
  {
    $num=(0xff >> $i) +1;
    if ($num & $_[0] ) {$bstr[$i-1] = "1";} else {$bstr[$i-1] = "0";};
  }
  return join("", @bstr);
}

#--------------------------------------------------------------------------
sub psegbin
{
  return join(".", pbin($_[0]), pbin($_[1]), pbin($_[2]), pbin($_[3]));
}

#--------------------------------------------------------------------------
sub pseg
{
  return join(".", $_[0], $_[1], $_[2], $_[3]);
}

#--------------------------------------------------------------------------
# Return the ip address in hex
sub pseghex
{
  return sprintf("%02x%02x%02x%02x", $_[0], $_[1], $_[2], $_[3]);
}

#--------------------------------------------------------------------------
# The /nn convention of printing netmasks just counts the number of bits
# which are set.
sub pmaskbits
{
  my $tot=0;
  my $bstr=$_[0];

  foreach $ch (split //, $bstr)
  {
    if ($ch == "1") { $tot++; }
  }
  return "/$tot";
}

#--------------------------------------------------------------------------
sub printclass
{
  my $cl=$_[0];
  print "net class  = ";

  if    ($cl==0)               { print "reserved\n"; }
  elsif ($cl==127)             { print "loopback\n"; }
  elsif ($cl>0 && $cl<127)     { print "A\n"; }
  elsif ($cl>=128 && $cl<=191) { print "B\n"; }
  elsif ($cl>=192 && $cl<=223) { print "C\n"; }
  elsif ($cl>=224 && $cl<=239) { print "D\n"; }
  elsif ($cl>=240 && $cl<=255) { print "Experimental\n"; }
  else                         { print "Invalid\n"; }
}

#--------------------------------------------------------------------------
sub netrange
{
  my @network = splice(@_,0,4);
  my @netmask = splice(@_,0,4);
# foreach $item (@_) { print "x $item\n"; }

  # Determine masked segment
  for ($seg=3; $seg>=0; $seg--)
  {
    if ($netmask[$seg] != "0")  {last}
  }

  $mask=$netmask[$seg];

  $range=256-$mask;
  print "Available subnets\n";
  printf("Netmask           = %d\n", $mask);
  printf("Subnets available = %d\n", $mask / $range +1);
  printf("Hosts per subnet  = %d\n", $range -2);

  for ($i=0; $i<=$mask; $i=$i+$range)
  {
    if    ($seg==0) { printf("%s.%s.%s.%s\n", $i, $network[1], $network[2], $network[3]); }
    elsif ($seg==1) { printf("%s.%s.%s.%s\n", $network[0], $i, $network[2], $network[3]); }
    elsif ($seg==2) { printf("%s.%s.%s.%s\n", $network[0], $network[1], $i, $network[3]); }
    elsif ($seg==3) { printf("%s.%s.%s.%s\n", $network[0], $network[1], $network[2], $i); }
  }

}

#--------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------
if ($#ARGV <1)
{
  print "Usage: subnet [-f]  \n";
  print "    -f print the full range of subnets for the netmask\n";
  print "\n";
  exit;
}

if ($ARGV[0] == "-f")
{
  $opt_range=1;
  shift;
}

@ip = split /\./, @ARGV[0];
@nm = split /\./, @ARGV[1];

# bitwise AND to get subnet
# use +0 to convert to numbers
$nw[0]=$ip[0] & $nm[0] +0;
$nw[1]=$ip[1] & $nm[1] +0;
$nw[2]=$ip[2] & $nm[2] +0;
$nw[3]=$ip[3] & $nm[3] +0;

# bitwise NOT to get host
$ho[0]=$ip[0] & ~$nm[0] +0;
$ho[1]=$ip[1] & ~$nm[1] +0;
$ho[2]=$ip[2] & ~$nm[2] +0;
$ho[3]=$ip[3] & ~$nm[3] +0;

# Output possible network ranges if required
if ($opt_range)
{
  netrange(@nw,@nm);
}

print "\n";
printclass(@ip[0]);
print "\n";
printf("ip address = %s\n", psegbin(@ip));
printf("netmask    = %s\n", psegbin(@nm));
printf("subnet     = %s\n", psegbin(@nw));
printf("host       = %s\n", psegbin(@ho));
print "\n";

printf("ip address = %s\n", pseg(@ip));
printf("netmask    = %s %s %s\n", pseg(@nm), pseghex(@nm), pmaskbits(psegbin(@nm)));
printf("subnet     = %s\n", pseg(@nw));
printf("host       = %s\n", pseg(@ho));
print "\n";