Re: Grep



bill@xxxxxxxxxxxxx wrote:
Hi All,

I'm new to PERL so please forgive this posting...

Perl please.... not PERL :)

I have a file that I'm trying to "grep" a bunch of things with.
There are multiple entries in the file (system.log).

For example... I'm looking to grab all the "Failed Authentications" and
place them into a file and periodically email me the results.

I've got the opening of the file OK.
I've been placing the contents into an array...OK

When I run my script all that I am getting back is the last line that
contains what I'm looking for.

Can someone show me the light? What is the correct syntax for what I'm
looking to do.

your issue isn't syntax related so far as I can tell (at first blush it
looks fine). the error is in your implementation of your solution...


#!/usr/bin/perl

my $sys_log="/var/log/system.log";

open (SYSTEMLOG, "$sys_log") or die "Error opening opening logfle:$!";

my $now=localtime;
my $failed = "";
my $dirserve = "";

for my $line (<SYSTEMLOG>){

chomp $line;

if ($line =~/Failed Authentications/){
$hostname = $line;
}

here, you overwrite whatever what was in $hostname with what is in
line....

if ($line =~/DirectoryService/){
$dirserve = $line;
}

same here with $dirserve

}

# Close open file handle;
close SYSTEMLOG;

my $dum_log="/Users/bill/Desktop/dummy.log";

open (DUMMY, ">>$dum_log") or die "Error opening opening logfle:$!";

print DUMMY "$now \n";
print DUMMY "$hostname \n";
print DUMMY "$dirserve \n";

now you print out the current contents of the vars...

close DUMMY;

to repair your script I would suggest either

1) pushing $lines onto @hostnames & @dirserve arrays and
then printing the arrays into your file at the end; or
2) open your output file before you loop through your syslog,
and as you find interesting lines write them out one by one

the difference between the approaches is that the first gives you more
control over how you format or layout your hostnames and dirserves.

-jp

.



Relevant Pages

  • Re: hostname <-> ip address
    ... If I use the following script to get ip address if i supply a hostname ... $ perl getdnsinfo.pl netscape.com ... All aliases resolve to the same IP address. ... Musica Dei donum optimi, trahit homines, trahit deos. ...
    (comp.lang.perl.misc)
  • Re: query check
    ... I wasn't sure if perl had a way of getting the user's hostname and I ... script is from the same domain as the one hosting the script and if ...
    (comp.lang.perl.misc)
  • Re: hostname <-> ip address
    ... If I use the following script to get ip address if i supply a hostname ... $ perl getdnsinfo.pl netscape.com ... All aliases resolve to the same IP address. ... address resolves to the primary domain name. ...
    (comp.lang.perl.misc)
  • Slow Performance When Using DBI, otherwise Not
    ... I have a Solaris 8 host running perl 5.8 and using DBI version 1.50 and ... If I run this query from a shell script, it completes in under 1 second, ... using a perl script it takes 5-10 seconds. ... SELECT hostname from xxxxx.adc_ait_hosts where status> 0 order by hostname ...
    (perl.dbi.users)