我目前正在编写一个脚本来检查 /var/log/secure 文件并报告当天有多少次失败的尝试。我需要以以下格式输出它:
Date: 03/15/10 Time: 10:30 Number of failed attempts: 8
到目前为止,这是我的代码(没有做太多事情)...我走在正确的轨道上吗?
#!/bin/bash
classGID=5000
passfile=/var/log/secure
for i in $(grep ${classGID} ${passfile} | cut -d: -f1)
do
date=$(grep $i{passfile} | cut -d: -f2)
echo "Date: ${date}"
done
答案1
#!/usr/bin/env bash
c=1
while read line
do
a=( $line )
d="${a[@]::3}"
logdate=$( date '+%F' -d"$d" )
if [[ $logdate == $old_logdate ]]; then
(( c++ ))
elif [[ $old_logdate != $logdate ]]; then
printf "%s\t%s\t%s%s\n" "Date: $old_logdate Number of failed attempts: $c"
old_logdate=$logdate
c=1
continue
else
printf "%s\t%s\t%s%s\n" "Date: $logdate Number of failed attempts: $c"
fi
done < <(grep 'authentication failure' /var/log/secure)
它跳过最后一条记录,我不知道为什么