Salve Revirando o baú para fazer uma limpeza por aqui, encontrei um script que faz o cálculo automático, entre outras coisas, do número de bits da netmask de uma subrede, na notação CIDR. Útil para que quer calcular este número na montagem de redes. O script não é meu, os créditos estão no próprio texto. Coloquei em: http://www.uri.reitoria.br/~darkstar/download/ipcalc Basta torná-lo executável, e fornecer o IP e a netmask. Exemplo de saída: IRIS:~% ipcalc 192.168.4.100 255.255.255.0 Address: 192.168.4.100 Netmask: 255.255.255.0 ==>> 24 Wildcard: 0.0.0.255 Network: 192.168.4.0/24 (Class C) Broadcast: 192.168.4.255 HostMin: 192.168.4.1 HostMax: 192.168.4.254 Hosts/Net: 254 (Private Internet RFC 1918) A saída é mais completa, cortei algumas coisas, mas dá pra ter idéia da utilidade da coisa. -- Atenciosamente, a seguir o script. #!/usr/bin/perl -w # 2/2000 krischan at jodies.cx # # 0.14 Release # 0.14.1 Allow netmasks given as dotted quads # 0.15 Colorize Classbits, Mark new bits in network # 0.16 25.9.2000 Accept / as first argument # Print tag in the script # 0.17 Bugfix # 0.18 Replace \n with in HTML to make konqueror work. Argh. # 0.19 HTML modified again to make Internet Exploder work. Argh ** 2 # Added -v Option # 0.2 New Tabular Format. Idea by Kevin Ivory # 0.21 # 0.22 Don't show -1 if netmask is 32 (Sven Anderson) # 0.23 Removed broken prototyping. Thanks to Scott Davis sdavis(a)austin-texas.net # 0.31 4/1/2001 Print cisco wildcard (inverse netmask). # Idea by Denis Alan Hainsworth denis(a)ans.net # 0.32 5/21/2001 - Accepts now inverse netmask as argument (Alan's idea again) # Fixed missing trailing zeros in sub/supernets # Warns now when given netmasks are illegal # Added option to suppress the binary output # Added help text # 0.33 5/21/2001 Cosmetic # 0.34 6/19/2001 Use default netmask of class when no netmask is given use strict; my $version = '0.34 6/19/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 @class = qw (0 8 16 24 4 5 5); my $allhosts; my $mark_newbits = 0; my $print_html = 0; my $print_bits = 1; my $print_only_class = 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"; my $h; # Host address foreach (@privmin) { $_ = &bintoint(&dqtobin("$_")); } foreach (@privmax) { $_ = &bintoint(&dqtobin("$_")); } if (! defined ($ARGV[0])) { &usage; exit(); } if (defined ($ARGV[0]) && $ARGV[0] eq "-help") { help(); } if (defined ($ARGV[0]) && $ARGV[0] eq "-b") { $ARGV[0] = "-n"; $print_bits = 0; } 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 "-c") { shift @ARGV; $print_only_class = 1; } if (defined ($ARGV[0]) && $ARGV[0] eq "-h") { shift @ARGV; $print_html = 1; $qcolor = '' ; $ncolor = ''; $bcolor = ''; $mcolor = ''; $ccolor = ''; $dcolor = ''; $break = ""; $private = "(Private Internet)"; print "\n"; print "\n"; } my $host = "192.168.0.1"; my $mask = ''; my $mask2 = ''; 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 Illegal value for ADDRESS ($arg[0])$ncolor\n"; } if (defined $arg[1]) { $mask = $arg[1]; if (! ($mask = is_valid_netmask($mask)) ) { print "$mcolor Illegal value for NETMASK ($arg[1])$ncolor\n"; } } else { # if mask is not defined - take the default mask of the network class $mask = $class[getclass(dqtobin($host))]; } if ($print_only_class) { print $class[getclass(dqtobin($host))]; exit 0; } if (defined ($arg[2])) { $mask2 = $arg[2]; if (! ($mask2 = is_valid_netmask($mask2)) ) { print "$mcolor Illegal value for second NETMASK ($arg[2])$ncolor\n"; } } else { $mask2 = $mask; } print "\n"; printline ("Address", $host , (&dqtobin($host),$mask,$bcolor,0) ); my $m = cidrtobin($mask); #pack( "B*",("1" x $mask) . ("0" x (32 - $mask)) ); print_netmask($m,$mask); print "=>\n"; $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 "\n\n"; } exit; } sub supernet { $m = cidrtobin($mask2); ##pack( "B*",("1" x $mask2) . ("0" x (32 - $mask2)) ); $n = $h & $m; print_netmask($m,$mask2); print "\n"; printnet($n,$mask2); } sub subnets { my $subnets = 0; my @oldnet; my $oldnet; my $k; my @nr; my $nextnet; my $l; $m = cidrtobin($mask2); ##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 "B32",$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) ); printline ("Wildcard", &bintodq(~$m) , (~$m,$mask2,$bcolor,0) ); } sub getclass { my $n = $_[0]; my $class = 1; while (unpack("B$class",$n) !~ /0/) { $class++; if ($class > 5) { last; } } return $class; } 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 = ~cidrtobin($mask); ##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); #} $type = getclass($n); if ($type > 5 ) { $type = "Undefined Class"; } 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; $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"; if ($print_bits) { 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]); print 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 cidrtobin { my $cidr = $_[0]; pack( "B*",(1 x $cidr) . (0 x (32 - $cidr)) ); } sub dqtocidr { my $dq = $_[0]; $b = &dqtobin($dq); my $cidr = 1; my $firstbit = unpack("B1",$b) ^ 1; while (unpack("B$cidr",$b) !~ /$firstbit/) { $cidr++; last if ($cidr == 33); } $cidr--; #print "CIDR: $cidr\n"; #print "DQ: $dq\n"; my $m = cidrtobin($cidr); #print "NM: " . bintodq($m) . "\n"; #print "NM2: " . bintodq(~$m) . "\n"; if (bintodq($m) ne $dq && bintodq(~$m) ne $dq) { print "$mcolor Corrected illegal netmask: $dq" . "$ncolor\n"; } return $cidr; } sub usage { print << "EOF"; Usage: ipcalc [-n|-h|-v|-help] [[/]] [NETMASK] ipcalc takes an IP address and netmask and calculates the resulting broadcast, network, Cisco wildcard mask, and host range. By giving a second netmask, you can design sub- and supernetworks. It is also intended to be a teaching tool and presents the results as easy-to-understand binary values. -n Don't display ANSI color codes -b Suppress the bitwise output -c Just print bit-count-mask of given address -h Display results as HTML -help Longer help text -v Print Version Examples: ipcalc 192.168.0.1/24 ipcalc 192.168.0.1/255.255.128.0 ipcalc 192.168.0.1 255.255.128.0 255.255.192.0 ipcalc 192.168.0.1 0.0.63.255 EOF } sub help { print << "EOF"; IP Calculator $version Enter your netmask(s) in CIDR notation (/25) or dotted decimals (255.255.255.0). Inverse netmask are recognized. If you mmit the netmask, ipcalc uses the default netmask for the class of your network. Look at the space between the bits of the addresses: The bits before it are the network part of the address, the bits after it are the host part. You can see two simple facts: In a network address all host bits are zero, in a broadcast address they are all set. The class of your network is determined by its first bits. If your network is a private internet according to RFC 1918 this is remarked. When displaying subnets the new bits in the network part of the netmask are marked in a different color. The wildcard is the inverse netmask as used for access control lists in Cisco routers. You can also enter netmasks in wildcard notation. Do you want to split your network into subnets? Enter the address and netmask of your original network and play with the second netmask until the result matches your needs. Questions? Comments? Drop me a mail... krischan at jodies.de http://jodies.de/ipcalc Thanks for your nice ideas to make this thing more useful: Hermann J. Beckers hj.beckers(a)kreis-steinfurt.de Kevin Ivory ki(a)sernet.de Frank Quotschalla gutschy(a)netzwerkinfo.de Sven Anderson sven(a)anderson.de Scott Davis sdavis(a)austin-texas.net Denis A. Hainsworth denis(a)ans.net EOF usage(); exit; } ************************************************************ From: Mauricio Portilho Cavalcanti To: rodrigo ; pereiraf ; Sent: Sunday, April 21, 2002 12:50 AM Subject: Re: (linux-br) servidor Vc tem que entender como isso funciona. Busque algum livro que te explique a teoria... vale a pena vc conhecer para não ficar "achando" e "chutando". Dou os seguintes exemplos para vc entender, na prática (por favor, isso é prática, baseando-se na teoria): 1) 192.168.1.0 com netmask 255.255.255.0 são 254 IP's, 192.168.1.1 ao 192.168.1.254. O 192.168.1.0 é dito como número da sua rede e o 192.168.1.255 é o broadcast. 2) 192.168.1.64 com netmask 255.255.255.192 são 62 IP's, 192.168.1.65 ao 192.168.1.126. O 192.168.1.64 é dito como a sua rede e o 192.168.1.127 é o broadcast. 3) 10.0.0.0 com netmask 255.255.0.0 são 65536 (256*256) IP's, 10.0.0.1 ao 10.0.255.254 são 65534 IP's, 10.0.0.1 ao 10.0.255.254. O 10.0.0.0 é dito como a sua rede e o 10.0.255.255 é o broadcast. O netmask serve para vc dizer quantos IP's vc tem na sua rede... de uma forma didática e para uso prático, sem muita teoria (digo que funciona assim para calcular o que vc quer, mas a teoria é mais complicada um pco), vc faria o seguinte: 1) netmask 255.255.255.0 com rede 192.168.1.0. Subtraia 256 - 0 (último número do netmask) 256 IP's ! Então vc diminui 2 IP's (um da rede - 0 e o outro do broadcast - 255) e tem 254 IP's a partir do 1 da rede 192.168.1.0 2) netmask 255.255.255.192 com rede 192.168.1.64. Subtraia 256 - 192 (último número do netmask) 64 IP's ! Então vc diminui 2 IP's (um da rede - 64 e o outro do broadcast - 127) e tem 62 IP's a partir do 65 da rede 192.168.1.64 3) netmask 255.255.0.0 com rede 10.0.0.0. Subtraia 65536 (256*256) - 0 (no caso vc pega os 2 últimos octetos). 65536 IP's ! Então vc diminui 2 IP's (um da rede - 0 e o outro do broadcast - 255) e tem 65534 IP's a partir do 1 da rede 10.0.0.0 (até 10.0.255.255) Repito que isso só se faz na prática e sugiro que vc leia um livro para pegar a teoria, números binários, etc. Mauricio Cavalcanti. ----- Original Message ----- From: "rodrigo" To: "pereiraf" ; Sent: 17 April, 2002 10:24 AM Subject: Re: (linux-br) servidor > Meu amigo .... Nunca use este IP porque ele não ira Funcionar , pois vc so > usa o Ip xxx.xxx.xxx.0 se for criar uma rota para alguma rede. > > O ip com final xxx.xxx.xxx.0 e um endereço reservado, como o xxx.xxx.xxx.255 > . > > Falow .... espero ter ajudado. > > Se tiver duvidas ainda me envie um e-mail em private. > > > ----- Original Message ----- > From: "pereiraf" > To: > Sent: Wednesday, April 17, 2002 7:18 AM > Subject: (linux-br) servidor > > > > Pessoal, um amigo meu falou que o IP do servidor deveria ser 192.168.1.0, > > zero no final, como ele sabe mais que eu, fique na duvida, porque sempre > > achei que zero fosse como curinga e não deveria ser usado, a nãos ser para ************************************************************** ************************************************************** ************************************************************** **************************************************************