#!/afs/athena/contrib/perl/p

%net = (153, "APhi",
	199, "Dorm:Huntington",
	202, "ADP",
	203, "AEPi",
	204, "BTP",
	205, "CP",
	206, "DTD",
	207, "DU",
	208, "ET",
	209, "Fenway",
	210, "LCA",
	211, "ND",
	212, "PDT",
	213, "Fiji",
	214, "pika",
	215, "PKS",
	216, "PKT",
	217, "PSK",
	218, "PLP",
	219, "SAE",
	220, "SC",
	221, "Student House",
	222, "SPE",
	223, "TC",
	224, "TEP",
	225, "TX",
	226, "WILG",
	227, "ZBT",
	228, "ZP",
	232, "ATO",
	233, "DKE",
	234, "DP",
	235, "KS",
	236, "PBE",
	237, "TDC",
	238, "Dorm:EC-East",
	239, "Dorm:MacGregor",
	240, "Dorm:McCormick",
	241, "Dorm:New House",
	242, "Dorm:Next House",
	243, "Dorm:Random",
	244, "Dorm:Senior House",
	245, "Dorm:Baker",
	246, "Dorm:Bexley",
	247, "Dorm:Burton",
	248, "Dorm:EC-West");

%srvc = (79, "Finger",
	 80, "WWW",
	 8001, "WWW8001",
	 6000, "X");

$|=1;
open(HOSTS, "/afs/athena/astaff/reference/hosts/hosts");

while(<HOSTS>){
    ($garbage, $ipaddr, $hostname, $machine, $os, $garbage) = split(/ ?: ?/, $_);
    $ipaddr=~/18\.(\d\d\d)\.(\d+\.\d+)/;
    $subnet = $1;  $last = $2;
    next if $last eq "0.252";
    next unless($lg = $net{$subnet});
    next if($os eq "EXEC" || $os eq "BOOT" || $os eq "SNMP"); # || $os eq 
    next if($hostname =~ /TESTER-/);
	
#	print("$hostnames in $lg running $os\n");
#    print("$hostname at $lg\n") if $os=~/LINUX/;
    ($hname, @garbage) = split(/,/, $hostname);
    $procs++;
    $opid = fork();
    if(!$opid){
	&checkservices($hname);
	exit;
    }
    if($procs > 10){
	for $i (1..5){
	    wait;
	    $procs--;
	}
    }
    $opsys{$os}++;
    $where{$lg}++;
    if($lg =~ /Dorm/){
	$dorms++;
    }
    else{
	$ilg++;
    }
    $tot++;
}


print("Dorms: $dorms\nILGs: $ilg\nTotal: $tot");
for $os (keys %opsys){
    print("OS-->$os:  $opsys{$os}\n");
}
#for $lg (sort keys %where){
#    print("LG-->$lg: $where{$lg}\n");
#}


sub checkservices {
    local($h) = @_;

    for $port (keys srvc){
	print("Checking $srvc{$port} on $h\n");
	&checkaservice($h, $port);
    }
}

sub checkaservice {
    local($them, $port) = @_;

    $AF_INET = 2;
    $SOCK_STREAM = 1;
    
    $sockaddr = 'S n a4 x8';
    
    chop($hostname = `hostname`);
    
    ($name,$aliases,$proto) = getprotobyname('tcp');
    ($name,$aliases,$port) = getservbyname($port,'tcp')
	unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) =
        gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
    
    $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);
    # Make the socket filehandle.
 
    if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { 
	print("...socket...");
    }
    else {
	print("OOps, socket failed\n");
    }
    
# Give the socket an address.

    if (bind(S, $this)) {
	print("...bind...\n");
    }
    else {
	print("Oops, bind failed\n");
    }
    
# Call up the server.
    $pid = fork();
    if(!$pid){
	alarm(10);
	if (connect(S,$that)) {
	    print("*****Found $srvc{$port} running on $them in $lg\n");
	}
	
	close(S);
	exit(0);
    }
    wait;
}


