Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found