#!/usr/athena/bin/perl

@clusters = ('barker-5', 'barker-6', 'M1-142', 'M2-225', 'M4-035', 'M4-167', 'M11-113', 'M11-116', 'M14S-010', 'M16-034', 'M37-312', 'M37-318', 'M37-332', 'M66-080', 'E51-007','W20-575');

foreach $cl (@clusters) {
$c = 0;
for($i=1;$i<140;$i++)
{
	print("$cl-$i\n");
	$alive = `/usr/etc/ping $cl-$i 1`;
#	print("A: $alive\n");
	if($alive=~/alive/){
		push(@machines, '$cl-$i');
#		&zme("Adding $cl-$i...\n");
		$n++;
		if($n%50 == 0){&zme("Total $n machines on list\nJust added $cl-$i\nTotal $aom with access apparently on\n");}
		print("Adding $cl-$i to machine list\n");
	$access = `xset -q -display $cl-$i:0`;
#	print("Acc: $access\n");
	if($access =~ /\w+/){
		print("********Machine $m seems to have access on!\n");
#		&zme("********Machine $cl-$i seems to have access on!\n");
		$who = `timeout 5 /usr/athena/bin/finger @$cl-$i`;
		($mach, $headers, @rest) = split(/\n/, $who);
		@whoon = ();
		foreach $_ (@rest)
		{
			($name, @garbage) = split;
			push(@whoon, $name);
		}
		$report .= "\n$cl-$i:  ";
		$aom++;
		foreach $user (@whoon) {
			$report .= "$user, ";
			}
		
#		&zme("It appears $person is logged in to $cl-$i\n");
}

#		$c = 0;
	}
	else{$c++;
#		print("Not a machine $i:  $c\n");
		if($c>10){
		print("Done with this cluster\n");
		last;
		}
	}
}
}
	&zme($report);
exit;
print("\n\nSuspect machines:\n");
foreach $m (@machines) {
	$access = `xset -q -display $m:0`;
	if($access!~/unable/){
		print("Machine $m seems to have access on!\n");
}
}

sub zme {
        local($message) = @_;
        open (ZME, "|zwrite -n -q -d -s 'AutoAccess Monitor' $ENV{'USER'}");
        print(ZME $message);
        close(ZME);
}
