[NBLUG/talk] netthing.pl

Scott Doty scott at ponzo.net
Tue Apr 8 06:04:59 PDT 2008


If you were to measure time-spent-coding as a divisor to time-saved, the
attached script would have a "leveraged" ratio in the thousands...

( Please be kind when critiquing the "netthing", it was written when my idea
of scoped variables in perl was to use "local()"... ;) )

And thanks to aw at sonic.net for mentioning that this wasn't on our public
shell host...I've added netthing.pl to Bolt, mostly in case folks forget
where they put it, and have a need to run the thing...

Run the script w/out options for usage.

Cheers!

 -Scott
-------------- next part --------------
#!/usr/bin/perl
#Scott's Net Thing
#Thu Sep 18 22:51:43 PDT 1997: Scott Doty <scott at sonic.net>


$Usage='Given an IP address and netmask, netthing computes interesting
information about that IP address\' network.

Usage:
    netthing.pl ip address[/bitcount] [netmask]

Examples:
	netthing.pl 10.10.10.127/26
	netthing.pl 10.10.10.68 255.255.224.0
	netthing.pl 0/25

The last example gives you "generic" information about a /25 network.
';

$net = shift;  if($net eq '') { die($Usage); }
$mask = shift || '255.255.255.0';

$bitcount=0;
if( $net =~ m#([\d\.]+)/(\d+)# )
	{
	$net = $1;
	$bitcount = $2;
	}

CIP::start();

if($bitcount>30 || $bitcount < 0)
	{
	warn(	"Bitcount must be between 0-32.\n",
			'Note: a /32 "network" (netmask 255.255.255.255) is just an',"\n",
			'IP host route.',"\n");
	die($Usage);
	}

if(0 != $bitcount)
	{
	$zbits = 32-$bitcount;
	$bits = (2**$bitcount-1) << $zbits;
	$mask = CIP::ntoa($bits);
	}

$net = CIP::ntoa(
			CIP::aton($net) & CIP::aton($mask)
			);

$bcast = CIP::end_of_net($net,$mask);

$t		= CIP::aton($net)+1;
$gw		= CIP::ntoa($t);
$t++;

$bitfirst=$t;
$first	= CIP::ntoa($bitfirst);

$bitlast=CIP::aton($bcast);
$last	= CIP::ntoa($bitlast-1);

$countusable = $bitlast - $bitfirst;

print <<"EOF";
Network:    $net
Netmask:    $mask
Gateway:    $gw
Broadcast:  $bcast
Usable address range:  $first - $last ($countusable addresses)
EOF

exit(0);

#####################
#
package CIP;
#Calculate IP (CIP)

##############
#
sub start
#initialize constants
{
$v4_32mask = -1;	#/32 mask
$v4_24mask = -256;	#/24 mask
1;
}

##############
#
sub aton
#inet_aton...sorta
{
local($octets) = shift;
local(@octets) = split(/\./,$octets);
local($tmp);

$tmp = 0;
$tmp += $octets[0] << 24;
$tmp += $octets[1] << 16;
$tmp += $octets[2] << 8;
$tmp += $octets[3];

return($tmp);
}
##############
#
sub ntoa
{
local($n) = shift;
local(@octets, $oc);

$n &= $v4_32mask;

for $oc (3,2,1,0)
	{
	$octets[$oc] = ( $n & 255 );
	$n = ($n >> 8);
	}
return(join('.', at octets));
}

##############
#
sub end_of_net
{
local($netocts)		= shift;
local($maskocts)	= shift;

local($net)		= aton($netocts);
local($mask)	= aton($maskocts);

local($flip) = $v4_32mask ^ $mask; # XOR
return(ntoa($flip+$net));
}

##############
#
sub unsmash_mask
{
local($m) = shift;
local($ret);

if('-' eq substr($m,0,1))
	{
	$m = 0-$m;
	$ret = $v4_24mask << $m;
	}
	else
	{
	$ret = $v4_24mask + $m;
	}

return(ntoa($ret));
}



More information about the talk mailing list