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?

In reply to Removing Duplicates from an Array by azatoth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.