每次整理安全日志都很麻烦,简单的编辑了一个脚本,实现的是统计每月***总数,每种***类型总数,以及***最高的10个ip,并记录在一个excel文档里。恳请各位大神指正

日志形式如下:

wKiom1LTv3yixQ-qAAFlH_BwV5o869.jpg

wKioL1LTv2_g4xx3AAGOMV5-ILA014.jpg

涉及到的客户敏感信息进行了处理


以下为具体解释:

#!/bin/bash


read -p "please input the path of your logfiles,The Default is current path.

(Warning:Do not exists any other files!): " path     #以path变量接受用户输入


echo "The tool is working,it's depends on your file size.The pid is $$,please wait..." #$$表示当前工作进程的进程号 在此表示此shell工作的进程号


countf=statisticslog.xls  #定义最后生成的文件


if [ -n "$path" ]     #-n 测试 path变量值是否为不为空

then

 directory=$path    

else

 directory=$PWD       #用directory 存储变量值

fi


for file in $directory/*    #用file变量循环路径下的所有文件,所以开头提示用户除了日志文件和此文件外不能有其他文件

do

 if [ "$file" != "$directory/*" ]     #测试file变量是否只是输入的路径名

 then

   cat $file >> "temp.txt" 2>/dev/null#“>>” #表示追加内容到新的文件 此表示内容追加到                                                                                                      #temp.txt文件 此为临时文件 所有工作完成后

                                              #会自动删除 下面同理

                                              #2>/dev/null 表示不显示错误信息 /dev/null

                                              #是个无底洞


 else

   echo "There are no files in your input path!" && exit 1  #打出错误并退出

 fi

done


sed -i '/^[^0-9]/d' temp.txt   #去掉非数字开头的行,-i 为直接执行,不输出在屏幕上,^[^0-9]正则表达式匹配不是以数字开头的行 d表示删除 /^[^0-9]/d 表示删除非数字开头的行


sed -i '/^$/d' temp.txt #去掉空行 ^$是正则表达式表示空行的结果


printf "This section is the total attacks order by month:\n" >> $countf  #“>>” 表示内容追加到前面定义的statisticslog.xls文件 下面同理


printf "Mounth\tSum\n" >> $countf  


#以下统计的是每个月的***总数 并排序


awk 'BEGIN{FS="-";}{a[$2] += 1;}END{for (i in a) printf("%d\t%d\n",i,a[i]);}' temp.txt | sort -nr -k 2 >> $countf


#awk: BEGIN后大括号里的工作表示在awk处理每一行之前进行 FS表示定义每一行的域分隔符 默认不设置为空格 这里用"-"作为每个域的分割

#中间的大括号表示每一行进行的工作 在此表示统计每个月的***总数  例如 2013-01-03 $2代表第二个域即01 $0则表示全部即2013-01-03

#END后大括号的工作表示在awk处理每一行之后进行 这里遍历数组a中的每一个元素即月份 打印在屏幕上 \t代表制表符 \n代表回车

#sort 是排序 -n表示以数字排序 例如2和10 不加-n会让10排在2前面  -r为倒序 -k 表示以第二个域进行排序 这里是以总***数排序


printf "\n*******************************************************\n" >> $countf


printf "This section is the total attacks order by ip,it's top 10:\n" >> $countf


printf "ip\tSum\n" >> $countf


#以下统计的是每个ip的***总数 并排序 取前10


awk '{a[$4] += 1;}END{for (i in a) printf("%s\t%d\n",i,a[i]);}' temp.txt | sort -nr -k 2 | head -10 >> $countf


#head -n #显示前n行


printf "\n*******************************************************\n" >> $countf


printf "This section is the total attacks order by sort,it's top 10:\n" >> $countf


printf "sort\tSum\n" >> $countf


#以下统计的是每种***类型的***总数 并排序 取前10


awk 'BEGIN{FS="'/''"}{a[$2] += 1;}END{for (i in a) printf("%s\t%d\n",i,a[i]);}' temp.txt | sort -nr -k 2 | head -10 >> $countf


#注意 这里使用单引号做分隔符,为的是取出***类型的全称 BEGIN里的形式。

#之所以这样写 因为单引号直接使用字符串输出,屏蔽了所有特殊字符。所以先用一对单引号保证/'不被转义 再通过外层的双引号利用/将'表现出来

#当然 下面更好理解

#awk -F "'" '{a[$2] += 1;}END{for (i in a) printf("%s\t%d\n",i,a[i]);}' temp.txt | sort -nr -k 2 | head -10 >> $countf

#-F 同样是设置域分隔符,shell中双引号没有单引号严格,会解释特殊字符的意思


rm -rf temp.txt #删除临时文件


echo "It's finished,enjoy your job!" && exit 0 #成功退出 exit 0 表示此次执行成功 0为成功 1 为失败


********************************我是快乐的分割线*************************************


脚本如下

#!/bin/bash

#made by ameng


read -p "please input the path of your logfiles,The Default is current path.

(Warning:Do not exists any other files!): " path


echo "The tool is working,it's depends on your file size.The pid is $$,please wait..."


countf=statisticslog.xls


if [ -n "$path" ]

then

 directory=$path

else

 directory=$PWD

fi


for file in $directory/*

do

 if [ "$file" != "$directory/*" ]

 then

   cat $file >> "temp.txt" 2>/dev/null

 else

   echo "There are no files in your input path!" && exit 1

 fi

done


sed -i '/^[^0-9]/d' temp.txt


sed -i '/^$/d' temp.txt


printf "This section is the total attacks order by month:\n" >> $countf


printf "Mounth\tSum\n" >> $countf


awk 'BEGIN{FS="-";}{a[$2] += 1;}END{for (i in a) printf("%d\t%d\n",i,a[i]);}' temp.txt | sort -nr -k 2 >> $countf


printf "\n*******************************************************\n" >> $countf


printf "This section is the total attacks order by ip,it's top 10:\n" >> $countf


printf "ip\tSum\n" >> $countf


awk '{a[$4] += 1;}END{for (i in a) printf("%s\t%d\n",i,a[i]);}' temp.txt | sort -nr -k 2 | head -10 >> $countf


printf "\n*******************************************************\n" >> $countf


printf "This section is the total attacks order by sort,it's top 10:\n" >> $countf


printf "sort\tSum\n" >> $countf


awk  'BEGIN{FS="'\''"}{a[$2] += 1;}END{for (i in a) printf("%s\t%d\n",i,a[i]);}' temp.txt | sort -nr -k 2 | head -10 >> $countf


rm -rf temp.txt


echo "It's finished,enjoy your job!" && exit 0


结果如下:

wKiom1LTyW-RCr1GAALe00ZgNjE579.jpg