On Fri, 12 Jul 2002, Radu Filip wrote:
MI>
MI> Am de facult un calcul IP si nu sunt sigur ca l-am facut corect:
MI>
MI> Datele problemei:
MI> "Once you plug your computer into the data jack on the wall you will
MI> be assigned an IP address in the range of 140.173.169.203 -
MI> 140.173.169.251"
MI> (nu se dau nici un fel de alte date).
Uite aici o solutie.
/me s-a saturat de facut calcule in facultate. Ii ajunge. Daca avem
computere la ce draq sa nu le folosim.
#!/usr/bin/perl -w
use strict;
my $version = '0.23 2/23/2001';
my $private = "(Private Internet RFC 1918)";
my @privmin = qw (10.0.0.0 172.16.0.0 192.168.0.0);
my @privmax = qw (10.255.255.255 172.31.255.255 192.168.255.255);
my $allhosts;
my $mark_newbits = 0;
my $print_html = 0;
my $qcolor = "\033[34m"; # dotted quads, blue
my $ncolor = "\033[m"; # normal, black
my $bcolor = "\033[33m"; # binary, yellow
my $mcolor = "\033[31m"; # netmask, red
my $ccolor = "\033[35m"; # classbits, magenta
my $dcolor = "\033[32m"; # newbits, green
my $break ="\n";
foreach (@privmin) {
$_ = &bintoint(&dqtobin("$_"));
}
foreach (@privmax) {
$_ = &bintoint(&dqtobin("$_"));
}
if (! defined ($ARGV[0])) {
&usage;
}
if (defined ($ARGV[0]) && $ARGV[0] eq "-v") {
print "$version\n";
exit 0;
}
if (defined ($ARGV[0]) && $ARGV[0] eq "-n") {
shift @ARGV;
$qcolor = '';
$ncolor = '';
$bcolor = '';
$mcolor = '';
$ccolor = '';
$dcolor = '';
}
if (defined ($ARGV[0]) && $ARGV[0] eq "-h") {
shift @ARGV;
$print_html = 1;
$qcolor = '<font color="#0000ff">' ;
$ncolor = '<font color="#000000">';
$bcolor = '<font color="#909090">';
$mcolor = '<font color="#ff0000">';
$ccolor = '<font color="#009900">';
$dcolor = '<font color="#663366">';
$break = "<br>";
$private = "(<a href=\"http://www.ietf.org/rfc/rfc1918.txt\">Private
Internet</a>)";
print "<pre>\n";
print "<!-- Version $version -->\n";
}
my $host = "192.168.0.1";
my $mask = "24";
my $mask2 = '24';
my @arg;
if ((defined $ARGV[0]) &&($ARGV[0] =~ /^(.+?)\/(.+)$/)) {
$arg[0] = $1;
$arg[1] = $2;
if (defined($ARGV[1])) {
$arg[2] = $ARGV[1];
}
} else {
@arg = @ARGV;
}
if (defined $arg[0]) {
$host = $arg[0];
}
if (! ($host = &is_valid_dq($host)) ) {
print "$mcolor Strange value for ADDRESS ($arg[0])$ncolor\n";
}
if (defined $arg[1]) {
$mask = $arg[1];
if (! ($mask = is_valid_netmask($mask)) ) {
print "$mcolor Strange value for NETMASK ($arg[1])$ncolor\n";
}
}
if (defined ($arg[2])) {
$mask2 = $arg[2];
if (! ($mask2 = is_valid_netmask($mask2)) ) {
print "$mcolor Strange value for second NETMASK ($arg[2])$ncolor\n";
}
} else {
$mask2 = $mask;
}
print "\n";
printline ("Address", $host , (&dqtobin($host),$mask,$bcolor,0)
);
my $m = pack( "B*",("1" x $mask) . ("0" x (32 - $mask)) );
print_netmask($m,$mask);
print "=>\n";
my $h = dqtobin($host);
my $n = $h & $m;
&printnet($n,$mask);
if ( $mask2 == $mask ) {
&end;
}
if ($mask2 > $mask) {
print "Subnets\n\n";
$mark_newbits = 1;
&subnets;
} else {
print "Supernet\n\n";
&supernet;
}
&end;
sub end {
if ($print_html) {
print "</pre>\n";
}
exit;
}
sub supernet {
$m = pack( "B*",("1" x $mask2) . ("0" x (32 - $mask2)) );
$n = $h & $m;
print_netmask($m,$mask2);
printnet($n,$mask2);
}
sub subnets {
my $subnets = 0;
my @oldnet;
my $oldnet;
my $k;
my @nr;
my $nextnet;
my $l;
$m = pack( "B*",("1" x $mask2) . ("0" x (32 - $mask2)) );
print_netmask($m,$mask2);
print "\n"; #*** ??
@oldnet = split //,unpack("B*",$n);
for ($k = 0 ; $k < $mask ; $k++) {
$oldnet .= $oldnet[$k];
}
for ($k = 0 ; $k < ( 2 ** ($mask2 - $mask)) ; $k++) {
@nr = split //,unpack("b*",pack("L",$k));
$nextnet = $oldnet;
for ($l = 0; $l < ($mask2 - $mask) ; $l++) {
$nextnet .= $nr[$mask2 - $mask - $l - 1] ;
}
$n = pack "B*",$nextnet;
&printnet($n,$mask2);
++$subnets;
if ($subnets >= 1000) {
print "... stopped at 1000 subnets ...$break";
last;
}
}
if ( ($subnets < 1000) && ($mask2 > $mask) ){
print "\nSubnets: $qcolor$subnets $ncolor$break";
print "Hosts: $qcolor" . ($allhosts * $subnets) . "$ncolor$break";
}
}
sub print_netmask {
my ($m,$mask2) = @_;
printline ("Netmask", &bintodq($m) . " == $mask2", ($m,$mask2,$mcolor,0) );
}
sub printnet {
my ($n,$mask) = @_;
my $nm;
my $type;
my $hmin;
my $hmax;
my $hostn;
my $p;
my $i;
$m = pack( "B*",("1" x $mask) . ("0" x (32 - $mask)) );
$nm = pack( "B*",("0" x $mask) . ("1" x (32 - $mask)) );
$b = $n | $nm;
$type = 1;
while (unpack("B$type",$n) !~ /0/) {
$type++;
}
if ($type > 5) {
$type = '';
} else {
$type = "Class " . chr($type+64);
}
$hmin = pack("B*",("0"x31) . "1") | $n;
$hmax = pack("B*",("0"x $mask) . ("1" x (31 - $mask)) . "0" ) | $n;
print "\n $mask \n";
$hostn = (2 ** (32 - $mask)) -2 ;
$hostn = 1 if $hostn == -1;
$p = 0;
for ($i=0; $i<3; $i++) {
if ( (&bintoint($hmax) <= $privmax[$i]) &&
(&bintoint($hmin) >= $privmin[$i]) ) {
$p = $i +1;
last;
}
}
if ($p) {
$p = $private;
} else {
$p = '';
}
printline ("Network", &bintodq($n) . "/$mask", ($n,$mask,$bcolor,1), "
($ccolor" . $type. "$ncolor)" );
printline ("Broadcast", &bintodq($b) , ($b,$mask,$bcolor,0) );
printline ("HostMin", &bintodq($hmin) , ($hmin,$mask,$bcolor,0) );
printline ("HostMax", &bintodq($hmax) , ($hmax,$mask,$bcolor,0) );
printf "Hosts/Net: $qcolor%-22s$ncolor",$hostn;
if ($p) {
print "$p";
}
print "$break$break\n";
$allhosts = $hostn;
}
sub printline {
my ($label,$dq,$mask,$mask2,$color,$mark_classbit,$class) = @_;
$class = "" unless $class;
printf "%-11s$qcolor","$label:";
printf "%-22s$ncolor", "$dq";
print formatbin($mask,$mask2,$color,$mark_classbit);
if ($class) {
print $class;
}
print $break;
}
sub formatbin {
my ($bin,$actual_mask,$color,$mark_classbits) = @_;
my @dq;
my $dq;
my @dq2;
my $is_classbit = 1;
my $bit;
my $i;
my $j;
my $oldmask;
my $newmask;
if ($mask2 > $mask) {
$oldmask = $mask;
$newmask = $mask2;
} else {
$oldmask = $mask2;
$newmask = $mask;
}
@dq = split //,unpack("B*",$bin);
if ($mark_classbits) {
$dq = $ccolor;
} else {
$dq = $color;
}
for ($j = 0; $j < 4 ; $j++) {
for ($i = 0; $i < 8; $i++) {
if (! defined ($bit = $dq[$i+($j*8)]) ) {
$bit = '0';
}
if ( $mark_newbits &&((($j*8) + $i + 1) == ($oldmask + 1)) ) {
$dq .= "$dcolor";
}
$dq .= $bit;
if ( ($mark_classbits &&
$is_classbit && $bit == 0)) {
$dq .= $color;
$is_classbit = 0;
}
if ( (($j*8) + $i + 1) == $actual_mask ) {
$dq .= " ";
}
if ( $mark_newbits &&((($j*8) + $i + 1) == $newmask) ) {
$dq .= "$color";
}
}
push @dq2, $dq;
$dq = '';
}
return (join ".",@dq2) . $ncolor;
;
}
sub dqtobin {
my @dq;
my $q;
my $i;
my $bin;
foreach $q (split /\./,$_[0]) {
push @dq,$q;
}
for ($i = 0; $i < 4 ; $i++) {
if (! defined $dq[$i]) {
push @dq,0;
}
}
$bin = pack("CCCC",@dq); # 4 unsigned chars
return $bin;
}
sub bintodq {
my $dq = join ".",unpack("CCCC",$_[0]);
return $dq;
}
sub bintoint {
return unpack("N",$_[0]);
}
sub is_valid_dq {
my $value = $_[0];
my $test = $value;
my $i;
my $corrected;
$test =~ s/\.//g;
if ($test !~ /^\d+$/) {
return 0;
}
my @value = split /\./, $value, 4;
for ($i = 0; $i<4; $i++) {
if (! defined ($value[$i]) ) {
$value[$i] = 0;
}
if ( ($value[$i] !~ /^\d+$/) ||
($value[$i] < 0) ||
($value[$i] > 255) )
{
return 0;
}
}
$corrected = join ".", @value;
return $corrected;
}
sub is_valid_netmask {
my $mask = $_[0];
if ($mask =~ /^\d+$/) {
if ( ($mask > 32) || ($mask < 1) ) {
return 0;
}
} else {
if (! ($mask = &is_valid_dq($mask)) ) {
return 0;
}
$mask = dqtocidr($mask);
}
return $mask;
}
sub dqtocidr {
my $dq = $_[0];
$b = &dqtobin($dq);
my $cidr = 1;
while (unpack("B$cidr",$b) !~ /0/) {
$cidr++;
last if ($cidr == 33);
}
$cidr--;
return $cidr;
}
sub usage {
print << "EOF";
Usage: ipcalc [-n|-h] <ADDRESS> <NETMASK> [NETMASK]
Calculates network parameters given by ADDRESS an NETMASK and displays
them as dotted quads and binary values. If a second NETMASK is provided
the resulting super- or subnets of a transition to the new netmask are
displayed. ADDRESS can be the ip-address in the network of interest or part
of the network prefix.
-n Don't display ANSI color codes
-h Display results as HTML
-v Print Version
Example:
ipcalc 192.168.0.1 24
EOF
exit;
}
/me s-a hotarit ca de azi sa raspunda doar cu solutii. Orice minune tine
doar 3 zile ... ....
--
best regards,
("`-''-/").___..--''"`-._ (\
Mihai Ichim `6_ 6 ) `-. ( ).`-.__.`)
mailto: [EMAIL PROTECTED] (_Y_.)' ._ ) `._ `. ``-..-'
http://www.medianets.it _..`--'_..-_/ /--'_.' ,'
(il),-'' (li),' ((!.-'
*************************************************
M E D I A N E T S
WWW : http://www.medianets.it
Email : [EMAIL PROTECTED]
NOI SIAMO LA SOLUTIONE !
*************************************************
---
Pentru dezabonare, trimiteti mail la
[EMAIL PROTECTED] cu subiectul 'unsubscribe rlug'.
REGULI, arhive si alte informatii: http://www.lug.ro/mlist/