每次整理安全日誌都很麻煩,簡單的編輯了一個腳本,實現的是統計每月***總數,每種***類型總數,以及***最高的10個ip,並記錄在一個excel文檔裏。懇請各位大神指正
日誌形式如下:
涉及到的客戶敏感信息進行了處理
以下爲具體解釋:
#!/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
結果如下: