Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  shell  >  正文 查看IP访问量的shell脚本汇总

查看IP访问量的shell脚本汇总

发布时间:2014-10-08   编辑:www.jquerycn.cn
本文介绍下,一些用于查看ip访问量的shell脚本,另外还有一些查看time_wait连接、syn连接等的脚本,适合分析网络状况。有需要的朋友,一定要参考下。

第一部分,
1,查看TCP连接状态
 

复制代码 代码示例:
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'
netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
netstat -ant|awk '/ip:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -n
netstat -ant|awk '/:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -rn|head -n 10
awk 'BEGIN{printf ("http_code\tcount_num\n")}{COUNT[$10]++}END{for (a in COUNT) printf a"\t\t"COUNT[a]"\n"}'

2,查找请求数请20个IP(常用于查找攻来源):
 

复制代码 代码示例:
netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n20

3,用tcpdump嗅探80端口的访问看看谁最高
 

复制代码 代码示例:
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20

4,查找较多time_wait连接
 

复制代码 代码示例:
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

5,找查较多的SYN连接
 

复制代码 代码示例:
netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more

6,根据端口列进程
 

复制代码 代码示例:
netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1

第二部分,网站日志分析(Apache):
1,获得访问前10位的ip地址
 

复制代码 代码示例:
cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
cat access.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}'

2,访问次数最多的文件或页面,取前20及统计所有访问IP
 

复制代码 代码示例:
cat access.log|awk '{print $11}'|sort|uniq -c|sort -nr|head -20
awk '{ print $1}' access.log |sort -n -r |uniq -c|wc -l

3,列出传输最大的几个exe文件(分析下载站的时候常用)
 

复制代码 代码示例:
cat access.log |awk '($7~/\.exe/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -20

4,列出输出大于200000byte(约200kb)的exe文件以及对应文件发生次数
 

复制代码 代码示例:
cat access.log |awk '($10 > 200000 && $7~/\.exe/){print $7}'|sort -n|uniq -c|sort -nr|head -100

5,如果日志最后一列记录的是页面文件传输时间,则有列出到客户端最耗时的页面
 

复制代码 代码示例:
cat access.log |awk '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100

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

复制代码 代码示例:
cat access.log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

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

复制代码 代码示例:
cat access.log |awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20

8,统计网站流量(G)
 

复制代码 代码示例:
cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'

9,统计404的连接
 

复制代码 代码示例:
awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort

10,统计http status.
 

复制代码 代码示例:
cat access.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'
cat access.log |awk '{print $9}'|sort|uniq -c|sort -rn

11,每秒并发:
 

复制代码 代码示例:
awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}'|sort -k 2 -nr|head -n10

您可能感兴趣的文章:
查看IP访问量的shell脚本汇总
shell脚本实现网站日志分析统计
自动统计网站访问日志的shell脚本
获取apache/lighttpd服务器中活跃用户的shell脚本
php会记录ip吗
解决PHP中Web程序中shell_exec()执行Shell脚本不成功问题
如何用shell脚本分析网站日志统计PV、404、500等数据
使用shell快速查看局域网中主机的IP地址的代码
bash shell脚本执行的几种方法
用于查询ip所在地的shell脚本

[关闭]