Perl Example:
#!/usr/bin/perl
# an exampple of a basic perl script in practice
# not commented on purpose here (very bad idea normally)
use strict;
use warnings;
$msgsf="/var/log/secure"; # scalar
$ignore="128.123.";
open(in,"grep 'Accepted password' $msgsf | grep -v $ignore |") || die "cannot open $msgsf $!\n";
while(<in>){
$ln++;
#Jan 12 03:04:05 host sshd[12345]: Accepted password for username from 111.33.44.55 port 54321 ssh2
#Aug 30 14:38:52 host sshd[223686]: Accepted keyboard-interactive/pam for username from 10.88.14.3 port 56418 ssh2
#0 1 2 3 4 5 6 7 8 9 10
if(/^(\w{3})\s+(\d+)/) {
$newdate = $1 . "_" . $2;
} else {
print STDERR "wrong date line $ln ...\n";
}
if($newdate ne $date) {
$date = $newdate;
print $date,"\n";
}
$daily{$date} +=1; # %daily - hash / associative array (of scalars $daily{'Aug_30'}, $daily{'Aug_31'}, ...)
@flds=split(/\s+/); # @flds - array (of scalars $flds[0], $flds[1], ...)
$userslogins{$flds[8]} +=1;
print "\t$flds[2] $flds[8] on $flds[3] from $flds[10]\n";
}
close(in);
print "\nDAILY:\n";
foreach $d (sort keys %daily){
print "$d $daily{$d}\n";
}
print "\nPER USER:\n";
foreach $u (sort keys %userslogins){
$un++;
print "$un $u $userslogins{$u}\n";
}
or better ?
#!/usr/bin/perl
$msgsf="/var/log/secure";
open(in,$msgsf) || die "cannot open $msgsf $!\n";
while(<in>){
$ln++;
next unless /Accept/;
@flds=split(/\s+/);
...
}