主要用于平时web日志的安全检查,护网还是直接看设备比较好
一、功能
- 列出当天不同IP的访问次数
- 查看某一个IP访问了哪些页面
- 查看当天有多少个IP访问
- 根据关键词筛选日志内容
二、效果图
0.刚运行脚本需要输入日志文件名或绝对路径
输入错误需要重新输入
1.可查看不同IP的访问次数
2.查看某一IP访问了哪些页面,下面还可以将该日志保存到文件中,按y保存,IP一定要写选项1里面有的IP
3.查看当天共有多少个IP访问
4.筛选出具有关键词的日志
三、代码
#!/bin/bash
# 字体颜色
Green_font_prefix="\033[32m"
Red_font_prefix="\033[31m"
Font_color_suffix="\033[0m"
# 分割线
line(){
for i in {1..100};do
if [ $i -ne 100 ];then
echo -ne "-"
else
echo -e "-"
fi
done
}
# 选项一
diffip(){
line
cut -d- -f 1 "$logfile"|sort| uniq -c | sort -rn
}
# 选项二
oneip(){
while true;do
read -rp "请输入需要分析的IP:" ip
line
if [ "$(grep ^$ip $logfile |wc -l)" -ne 0 ];then
grep ^$ip $logfile| awk '{print $4."]",$1,$7,$9}'
break
else
echo -e "${Red_font_prefix}未找到该IP!请重新输入!!${Font_color_suffix}"
line
continue
fi
done
}
# 保存文件
savefile(){
read -rp "是否保存到文件中(y/n):" choose
if [ $choose == "y" ] || [ $choose == "Y" ];then
grep ^$ip $logfile| awk '{print $4."]",$1,$7,$9}' > "$ip.txt"
echo -e "已保存到$ip.txt中"
fi
}
# 选项三
howmanyip(){
line
awk '{print $1}' $logfile|sort|uniq|wc -l
}
# 选项四
keyword(){
while true;do
line
read -rp "请输入关键词(q为退出):" key
if [ $key == "q" ];then
break
fi
more "$logfile" | grep "$key"
done
}
while true;do
read -rp "请输入需要分析的日志:" logfile
find "$logfile" &>/dev/null
if [ $? -ne 0 ];then
echo -e "${Red_font_prefix}未找到日志,请输入文件名或绝对路径!!(家目录要写全称而不是~)${Font_color_suffix}"
line
else
break
fi
done
# *****************************************
# ************** 主 面 板****************
# *****************************************
line
while true; do
echo -e "web中间件日志分析脚本 ${Red_font_prefix}[v1.0]${Font_color_suffix}
${Green_font_prefix}1.${Font_color_suffix} 列出当天不同IP的访问次数
${Green_font_prefix}2.${Font_color_suffix} 查看某一个IP访问了哪些页面
${Green_font_prefix}3.${Font_color_suffix} 查看当天有多少个IP访问
${Green_font_prefix}4.${Font_color_suffix} 根据关键词筛选日志内容
${Green_font_prefix}5.${Font_color_suffix} 退出脚本
"
read -r -p "请输入数字 [1-5]:" num
if [ "$num" -eq "1" ];then
diffip
elif [ "$num" -eq "2" ];then
oneip
savefile
elif [ "$num" -eq "3" ];then
howmanyip
elif [ "$num" -eq "4" ];then
keyword
elif [ "$num" -eq "5" ];then
echo ""
echo -e "${Green_font_prefix}
*****************************************
*********** 谢谢您的使用,再见************
*****************************************
${Font_color_suffix}"
exit 0
else
echo -e "${Red_font_prefix}请输入正确数字!!${Font_color_suffix}"
line
continue
fi
line
done
四、运行脚本
方法一
sh 脚本名
方法二
bash 脚本名
方法三
chmod 777 脚本名
./脚本名 (这个要在脚本同一个目录才可以)
有的需求没有可以自己在代码上加,挺简单的