Linux中部署tomcat 日志显示访问者IP地址
tomcat日志文件访问IP统计
server.xml文件默认配置
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
如果想在日志文件中显示访问者的IP,需要修改配置如下
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>
访问日志命令:
[root@iZ2zeab8t820b5yseZ logs]# tail -f localhost_access_log.2022-03-11.txt
tomcat日志文件访问IP统计
访问前10的IP以及对应IP访问次数降序排列(因IP在日志中的第一列,所以取$1)
[root@iZ2zeab8t820feZ logs]# cat localhost_access_log.2022-03-11.txt|awk '{print $1}'|sort |uniq -c|sort -nr|head -10
shell脚本1:统计某日志文件中所有访问者的IP
awk '{aaa[$1]++;} END{for(i in aaa) { printf("%s\t%s\n", aaa[i], i); }}' localhost_access_log.2022-03-11.txt | sort -bn
grep -i -o -E -r -e "([0-9]{1,3}.){3}[0-9]{1,3}" localhost_access_log.2018-09-19.txt | sort -n | uniq -c | sort -n
shell脚本2:统计某日志文件中所有访问者的IP
[root@iZ2zeab8t820b5ywp0rkfeZ logs]# awk '{print $1}' localhost_access_log.2022-03-11.txt | grep -E "([0-9]{1,3}.){3}[0-9]{1,3}" | sort | uniq -c | sort -g