http://qs321.pair.com?node_id=113082

azatoth has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i am hacking a script together to run on an NT box. It's a security script that checks whether someone is logged in using a forbidden userId ("admin") on a given unix box. The script works fine, logs in to the unix host and performs a "who", then runs "nbtstat" on NT to derive the NT logins. From these we can get their mail addresses and send them a nasty email. The %userData hash is already initialised as we know which users can log in to the box...

Anyhoo, here's my code, and the problem I'm having is weeding out duplicates from @mailArray so no more than one email is sent (Users could be logged in more than once with the same userId to the unix box). Hope you can help me, gawd I do.

#!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 duplic +ates unlink("naughtyUsers.txt") or warn "Unlink warning : $!\n"; my ($telnet,@naughtyList,$ip,$userName,$mailUserName,@whoArray, @n +btstatInfo,@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, "<naughtyUsers.txt") or die "Died @ Open : $!\n"; #initialise userData hash my %userData =( NTUSERONE => "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 = <FH>; 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 }

Azatoth a.k.a Captain Whiplash

Make Your Die Messages Full of Wisdom!
Get YOUR PerlMonks Stagename here!
Want to speak like a Londoner?