#!perl -w use strict; use Net::Telnet; ##script to see how many people are still logging in ##as admin. get the ip/origination, and crucify them ##with a particularly condescending email #get rid of previous naughtyUsers.txt, so we don't have any duplicates unlink("naughtyUsers.txt") or warn "Unlink warning : $!\n"; my ($telnet,@naughtyList,$ip,$userName,$mailUserName,@whoArray, @nbtstatInfo,@mungeData,$ntID); #log in to machine $telnet = new Net::Telnet ( Timeout => 120); $telnet->open("hostname.company.com") or die "Died @ Open : $!\n"; $telnet->login( Name => "username", Password => "password", ) or die "Died @ Login : $!\n"; @whoArray = $telnet->cmd("who") or die "Died @ Who : $!\n"; $telnet->close; #get relevant data - i.e. glb1prd users foreach my $line(@whoArray) { if ($line =~ /^admin/) { push(@naughtyList, $line); } } #now resolve ip to get name - ouch #we are going to redirect stdout, so save it open (SAVEDOUT, ">&STDOUT"); open (SAVEDERR, ">&STDERR"); foreach my $foo (@naughtyList) { ($ip = $foo) =~ s/.*\((.*)\)/$1/g; open (STDOUT, ">>naughtyUsers.txt") or die "Died @ Open : $!\n"; open (STDERR, ">&STDOUT") or die "Died @ Open : $!\n"; system("nbtstat -A $ip") if ($ip =~ /^\d+/); close (STDOUT); close (STDERR); #set stdout back to default open (STDOUT, ">&SAVEDOUT"); open (STDERR, ">&SAVEDERR"); } #now munge data in file to get simple lines of who's logged in open (FH, " "ntuserone\@company.com", NTUSERTWO => "ntusertwo\@company.com", NTUSERTHREE => "ntuserthree\@company.com", NTUSERFOUR => "ntuserfour\@company.com", NTUSERFIVE => "ntuserfive\@company.com", ); #shove everything from naughtyUsers.txt into array @mungeData = ; foreach my $entry(@mungeData) { foreach my $key (keys %userData) { ( ($key, my $value) = each %userData); if ($entry =~ /^$key/) { push (my @mailArray, $value); mailNaughtyUser(@mailArray); } } } sub mailNaughtyUser { my @addies = shift; foreach my $address(@addies) { #here's my problem - need to ensure duplicates #are pop()'d out of the array before we send a mail }