首页 > 技术文章 > linux access.log awk统计ip

jackduan 2017-10-24 11:28 原文

 同样ip从多到少排序统计  

cat logs/test.com_access_log.20171024 |awk '{print $1}'| sort | uniq -c |sort -nr |less

 

总共访问ip统计

 cat logs/test.com_access_log.20171024 |awk '{print $1}'|uniq -c |awk '{sum+=$1}END{print sum}'

cat logs/test.com_access_log.20171023 |awk '{print $1}'|uniq -c | awk '{print a=a+$1}end{print a}' | tail -n1

 

统计访问前十的ip

awk '{print $1}' logs/test.com_access_log.20171024|sort |uniq -c|sort -nr|head -n 10

 http://7177526.blog.51cto.com/7167526/1385469

我想统计今天产生的日志进行分析,也就是15/Aug/2011  
我想出来的结果:awk '$4 ~/15\/Aug/ {print}' access_log

172.16.119.8 - admin [15/Aug/2011:18:17:50 +0800] "PROPFIND /svn/EAGLE HTTP/1.1" 207 649
172.16.119.8 - admin [15/Aug/2011:18:17:50 +0800] "PROPFIND /svn/EAGLE/!svn/vcc/default HTTP/1.1" 207 401
172.16.119.8 - admin [15/Aug/2011:18:17:50 +0800] "PROPFIND /svn/EAGLE/!svn/bln/31 HTTP/1.1" 207 454
172.16.119.8 - admin [15/Aug/2011:18:17:50 +0800] "PROPFIND /svn/EAGLE HTTP/1.1" 207 649
172.16.119.8 - admin [15/Aug/2011:18:17:50 +0800] "PROPFIND /svn/EAGLE/!svn/vcc/default HTTP/1.1" 207 454

 

awk '$4 ~/27\/Mar\/2014/ {print $1}' access_log |uniq -c|awk '{sum+=$1}END{print sum}'

统计同访问量

/27\/Mar\/2014/      表示 统计2014-3-17日的总数

/Mar\/2014/   表示统计2014年3月正个月的访问量

awk '{sum+=$1}END{print sum}'   表示对第一列数值求和,也就得到访问量了

wKiom1Mz4rHBQGwNAAFqIgPohI4829.jpg

awk '$4 ~/27\/Mar\/2014/ {print $1}' access_log |uniq -c| wc -l      统计不同IP个数

 

bianliang=24/Feb/2014;awk -v bianliang=$bianliang '$4~bianliang' access_log

wKioL1Mz-yGS3q48AADVdC5JtgI025.jpg

 

TODAY=$(date +%d/%b/%Y);grep $TODAY access_log

 

例如:

#cat 1.txt
6
8
8
12
15
17
141

#cat 1.txt | awk '{print a=a+$1}end{print a}' | tail -n1
207

或者:awk '{sum+=$1}END{print sum}'  filename

 

将数字放到文本文件a中
将下面的代码拷贝到b中
执行b得出结果
#!/bin/bash
result=0;
for num in `cat a`
do
   let result=$result+$num;
done
echo $result

 

 

通过apache 访问日志access.log 统计IP 和每个地址访问的次数,按访问量列出 前10 名

 

cat access_log |awk '{print $1}'|uniq -c |sort -rn |head -10  

 

apache 日志分析

 

 

1、在apachelog中找出访问次数最多的10个IP。

awk '{print $1}' apache_log |sort |uniq -c|sort -nr|head -n 10

awk 首先将每条日志中的IP抓出来,如日志格式被自定义过,可以 -F 定义分隔符和 print指定列;
sort进行初次排序,为的使相同的记录排列到一起;
upiq -c 合并重复的行,并记录重复次数。
head进行前十名筛选;
sort -nr按照数字进行倒叙排序。


2、在apache日志中找到访问最多的页面:
awk '{print $11}' apache_log |sed 's/^.*cn\(.*\)\"/\1/g'|sort |uniq -c|sort -rn|head


3、在apache日志中找出访问次数最多的几个分钟。
awk '{print  $4}' access_log |cut -c 14-18|sort|uniq -c|sort -nr|head

 

4、在apache日志中找到访问最多的页面:
awk '{print $11}' apache_log |sed 's/^.*cn/(.*/)/"//1/g'|sort |uniq -c|sort -rn|head

 

5、在apache日志中找出访问次数最多(负载最重)的几个时间段(以分钟为单位),然后在看看这些时间哪几个IP访问的最多?

版本1
#!/bin/bash
# analysis apache access log
# histroy
# caoyameng  version0.1 2010/01/24

if (test -z $1) ;then
read -p "Specify  logfile:" LOG
else
       LOG=$1
fi

if [ ! -e $LOG ];then
echo "I cann't find apache log file."
exit 0
fi

awk '{print  $4}' $LOG |cut -c 14-18|sort|uniq -c|sort -nr|head  >timelog
for   i in  `awk '{print $2}' timelog`
do

all=`grep $i timelog|awk '{print $1}'`
echo  " $i  $all"
IP=`grep $i $LOG| awk '{print $1}' |sort |uniq -c|sort -nr|head`
echo  "$IP"

done
rm  -f timelog

 

另一个版本的解决方法,其实就是换了下for的计算方式

#!/bin/bash
# analysis apache access log
# histroy
# caoyameng  version0.2 2010/01/24

if (test -z $1) ;then
read -p "Specify  logfile:" LOG
else
       LOG=$1
fi

if [ ! -e $LOG ];then
echo "I cann't find apache log file."
exit 0
fi

awk '{print  $4}' $LOG |cut -c 14-18|sort|uniq -c|sort -nr|head  >timelog
for (( i=1; i<=10; i=i+1 ))
do
num=`sed -n "${i}p" timelog|awk '{print $1}'`
time=`sed -n "${i}p" timelog|awk '{print $2}'`
echo  "####The No.$i "
echo  " "
echo  " $time   $num"
echo  " "
full=`grep $time $LOG| awk '{print $1}' |sort |uniq -c|sort -nr|head`
echo  "$full"
echo " "
done
rm  -f timelog


==================================================================

1,查看apache进程:
ps aux | grep httpd | grep -v grep | wc -l    // ps aux是显示所有进程和其状态。

 

2,查看80端口的tcp连接:
netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l

 

3,通过日志查看当天ip连接数,过滤重复:
cat access_log | grep "19/May/2011" | awk '{print $2}' | sort | uniq -c | sort -nr

 

4,当天ip连接数最高的ip都在干些什么(原来是蜘蛛):

cat access_log | grep "19/May/2011:00" | grep "61.135.166.230" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10

 

5,当天访问页面排前10的url:

cat access_log | grep "19/May/2010:00" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10

 

6,用tcpdump嗅探80端口的访问看看谁最高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr


接着从日志里查看该ip在干嘛:
cat access_log | grep 220.181.38.183| awk '{print $1"\t"$8}' | sort | uniq -c | sort -nr | less


7,查看某一时间段的ip连接数:
grep "2006:0[7-8]" www20110519.log | awk '{print $2}' | sort | uniq -c| sort -nr | wc -l

 

8,当前WEB服务器中联接次数最多的20条ip地址:

netstat -ntu |awk '{print $5}' |sort | uniq -c| sort -n -r | head -n 20

 

9,查看日志中访问次数最多的前10个IP
cat access_80_log |cut -d ' ' -f 1 |sort |uniq -c | sort -nr | awk '{print $0 }' | head -n 10 |less

 

10,查看日志中出现100次以上的IP

cat access_log |cut -d ' ' -f 1 |sort |uniq -c | awk '{if ($1 > 100) print $0}'| sort -nr |less

 

11,查看最近访问量最高的文件

cat access_log |tail -10000|awk '{print $7}'|sort|uniq -c|sort -nr|less

 

12,查看日志中访问超过100次的页面

cat access_log | cut -d ' ' -f 7 | sort |uniq -c | awk '{if ($1 > 100) print $0}' | less

 

13,列出传输时间超过 30 秒的文件

cat access_log|awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20

 

14,列出最最耗时的页面(超过60秒的)的以及对应页面发生次数

cat access_log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

推荐阅读