#!/usr/local/bin/perl
#change to where your perl is located

#please do not change and redistribute without contacting me at
# gnarf@octane.net
#await approval.

#this array holds machine names.. if you dont wish to use these,
#you can uncomment the next set of comments to check nidump hosts /
#or just set machines in this line..
@machines=("earth","mars","jupiter","etc");
#print STDOUT "Checking nidump for hosts: ";
#open (NIDUMP, "nidump hosts /|");
#while (<NIDUMP>) { 
#  chomp;
#  /^\d+.\d+.\d+.\d+\s+(\w+)/;
#  push @machines, $1;
#  print STDOUT "$1 ";
#}
#print STDOUT "\n";
close(NIDUMP);

while (1) {

foreach $machine (@machines) {
open(FINGER,"finger \@".$machine."|");
while(<FINGER>) {
  chomp;
  push @data, "$_\n";
}
close(FINGER);
}


foreach $inline (@data) {
  $_ = $inline;
  chop;
  $datalines=1;
  #really shouldnt do this.. i dunno why it does :P
  if (/\[(\w+)$/) {
    $machine=$1;
    print $machine.": error, connection timed out, removing from machines\n";
    $j=0;
    foreach $test (@machines) { if ($test eq $machine) {$i=$j;} $j++;}
    splice(@machines,$i,1);
  } else {
    if (/\[(\S+)\]/) { # detects [xenon] type of thing 
      $machine=$1;
      #add check for connect timed out now...
      if ($datalines==0) {
        print $machine.": error, connection timed out, removing from machines\n";
        $j=0;
        foreach $test (@machines) { if ($test eq $machine) {$i=$j;} $j++;}
        splice(@machines,$i,1);
      }
      $datalines==0;
    } else 
    {
      $datalines++;
      $_=$inline;
      if (/^No one/) { 
#          print $machine.": ".$_;
      } else { 
        if (!(/^Login/)) { # IGNORE THE HEADER 
          $data=$inline;
          $user=substr($data,0,8);
          $name=substr($data,9,20);
          $loc=substr($data,30,4);
          $_=$loc;
          if (/\s*(\S+)\s*/) { $loc=$1; }
          $idl=substr($data,33,5);
          $_=$idl;
          if (/\s*(\S+)\s*/ && ($loc ne "co")) { $loc="$loc($1)"; }
          push @newdata, join("%",($machine,$user,$name,$loc));
        }
      }
    }
  }
}
   if ($datalines==0) {
    print $machine.": error, connection timed out, removing from machines\n";
    $j=0;
    foreach $test (@machines) { if ($test eq $machine) {$i=$j;} $j++;}
    splice(@machines,$i,1);
   }
@users=sort {((split("%",$a))[1] cmp (split("%",$b))[1] 
|| (split("%",$a))[0] cmp (split("%",$b))[0]
|| (split("%",$a))[3] cmp (split("%",$b))[3]
)} @newdata;
$oldname="asdfads3124";
$oldmach="sdafdasf324";
foreach $line (@users) {
  @data=split("%",$line);
  if ($oldname ne $data[1]) {
   if ($oldname ne "asdfads3124") {print "\n\n";}
   $oldname=$data[1];
   $oldmach=$data[0];
   print "User: $data[1] Name: $data[2]\n";
   print "$data[0]:$data[3]";
  }
  else { if ($oldmach ne $data[0]) {
   $oldmach=$data[0];
   print "  $data[0]:$data[3]";
  }
  else {
  print ",$data[3]";
  }}
}
@data=();
@newdata=();
print "\n";
sleep 30;
system "clear";
}
