#!/bin/perl
#Simple class C and D scanner in perl
#Copyright (C) 1997 gilbert@pgci.ca

#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 (at your option) 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.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

sub usage {
print "Syntax: scanbc [-c][-d] [a.b.c.d] or [evil.com]\n";exit(1);} 

sub flush {  
    local($old) = select(shift);
    $| = 1;
    print "";
    $| = 0;
    select($old);
}

if (@ARGV == 0) {&usage;}

while (@ARGV > 0) {
$switch = shift(@ARGV);
$net = shift(@ARGV);

	if ($net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) {
	($a,$b,$c,$d) = split(/\./, $net);
		 if (($c eq "") || ($c > 256)) {
		 $c = 0;
		 }
		 if (($d eq "") || ($d > 256)) {
   		 $d = 0;
		 }
	}
        # if host is name, not ip address
        else {
              ($host, $aliases, $type, $len, @ip) = gethostbyname($net);
              ($a,$b,$c,$d) = unpack('C4',$ip[0]);
             }

 if ($switch eq "-c") {
     	for ($classc=$c;$classc<256;$classc++){
     		for ($classd=$d;$classd<256;$classd++){
     		$host = "$a.$b.$classc.$classd";     
		@addr = split(/\./, $host);
     		$addr = pack(' C4', @addr[0], @addr[1], @addr[2], @addr[3]);
     		($name, $aliases, $type, $len, @addrs) = gethostbyaddr($addr, 2);
     			if ($name ne "") {
     			print STDOUT "$host \=\> $name\n";
     			&flush(STDOUT);
			}
		}
        }
 }
 if ($switch eq "-d") {
     for ($classd=$d;$classd<256;$classd++) {
     $host = "$a.$b.$c.$classd";
     @addr = split(/\./, $host);
     $addr = pack(' C4', @addr[0], @addr[1], @addr[2], @addr[3]);
     ($name, $aliases, $type, $len, @addrs) = gethostbyaddr($addr, 2);
     	if ($name ne "") {
     	print STDOUT "$host \=\> $name\n";
    	&flush(STDOUT);
	}
     }
 }
}



