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

一些用于查看ip访问量的shell脚本,另外一些还可以查看time_wait连接,syn连接的脚本,可以用来分析网络的状况.。

首先我的nginx的access日志文件是存放在我的Linux机器上的:/var/log/nginx/sc/access.log

目录

1、首先第一部分:网站日志分析(nginx日志)

1、获取访问前10为的ip地址

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

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

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

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

6、统计网站流量(G)

7、统计404的连接

8、统计http status

2、第二部分:与连接相关的

1、查看TCP连接状态

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

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

4、根据端口列进程

5、查找较多time_wait连接

6、找查较多的SYN连接

1、首先第一部分:网站日志分析(nginx日志)

1、获取访问前10为的ip地址

三种方法:

sort -nr 是指将数字进行倒叙排序,-n是按照数字排序,-r是指倒叙排序。

sort -k2 -nr :k2按照第二列进行排序,-nr:是指将数字进行倒叙排序。

uniq -c去重:其中-c是count:检查文件并删除文件中重复出现的行,并在行首显示该行重复出现的次数。

第一种:cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10

其中的cat access.log将文本内容打印到屏幕。grep -v "^$" 去除空行。awk -F" " '{print $1}:表示用空格作为分隔符进行分割,打印出第1列。 sort 进行排序,默认是按照ascii码进行排序的。

-d, --delimiter delim:指定在-f参数中的field-list的分割符(为delim中的第一个字符,缺省为TAB).

awk -F ' ':指定分隔符为空格

第二种:cat access.log |grep -v "^$"|awk -F ' ' '{print $1}'|sort|uniq -c|sort -n -r|head -n 10

第三种:cut -d" " -f1 access.log |sort|uniq -c|sort -nr|head -1

[root@nginx-kafka01 sc]# cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
     22 192.168.2.123
     10 192.168.2.114
      5 192.168.2.135
      3 192.168.2.124
      2 192.168.2.148
      2 192.168.2.145
      1 192.168.2.112
      1 192.168.2.104

[root@nginx-kafka01 sc]# cat access.log |grep -v "^$"|awk -F ' ' '{print $1}'|sort|uniq -c|sort -n -r|head -n 10
     22 192.168.2.123
     10 192.168.2.114
      5 192.168.2.135
      3 192.168.2.124
      2 192.168.2.148
      2 192.168.2.145
      1 192.168.2.112
      1 192.168.2.104

[root@nginx-kafka01 sc]# cut -d" " -f1 access.log |sort|uniq -c|sort -nr|head -10
     22 192.168.2.123
     10 192.168.2.114
      5 192.168.2.135
      3 192.168.2.124
      2 192.168.2.148
      2 192.168.2.145
      1 192.168.2.112
      1 192.168.2.104

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

cat access.log|awk '{print $11}'|sort|uniq -c|sort -nr|head -20

[root@nginx-kafka01 logs]# cat access.log|awk '{print $11}'|sort|uniq -c|sort -nr|head -20
     71 "-"
     16 "http://192.168.119.144/"
     12 "http://192.168.2.152/"
      6 "http://192.168.119.145/"
      5 "http://www.sc.com/"
      3 "http://192.168.119.152/"

统计访问的ip地址一共有多少个:awk '{ print $1}' access.log |sort -n -r |uniq -c|wc -l

[root@nginx-kafka01 logs]# awk '{ print $1}' access.log |sort -n -r |uniq -c|wc -l
5

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

cat access.log |awk '($7~/\.exe/){print $10,$1,$4,$7}'|sort -nr|head -2

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

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

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

NF指的是最后一个字段。NR是指定行号。FS是指定分隔符。OFS是

 cat access.log |awk '($7~/\.php/){print $NF ,$1,$4,$7}'|sort -nr|head -10

6、统计网站流量(G)

cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'

[root@nginx-kafka01 sc]# cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'
1.01496e-05

7、统计404的连接

awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort

[root@nginx-kafka01 logs]# awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort
404 /favicon.ico
404 /favicon.ico
404 /favicon.ico
404 /favicon.ico
404 /favicon.ico
404 /favicon.ico
404 /favicon.ico
404 /favicon.ico
404 /sc.html
404 /v2-d57147dc41ee118a86f03bfb20b1ce25_r.jpg?source=1940ef5c
404 /v2-d57147dc41ee118a86f03bfb20b1ce25_r.jpg?source=1940ef5c
404 /v2-d57147dc41ee118a86f03bfb20b1ce25_r.jpg?source=1940ef5c

8、统计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
[root@nginx-kafka01 sc]# cat access.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'
304 26
200 12
404 8
[root@nginx-kafka01 sc]# cat access.log |awk '{print $9}'|sort|uniq -c|sort -rn
     26 304
     12 200
      8 404

2、第二部分:与连接相关的

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/ {++S[$NF]};END {for(a in S) print a, S[a]}'
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
[root@nginx-kafka01 logs]# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
      8 LISTEN
      1 Foreign
      1 ESTABLISHED
      1 established)
[root@nginx-kafka01 logs]# netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
ESTABLISHED 1
[root@nginx-kafka01 logs]# netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
ESTABLISHED 1
[root@nginx-kafka01 logs]# netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
ESTABLISHED 	 1
[root@nginx-kafka01 logs]# netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
      1 ESTABLISHED
[root@nginx-kafka01 logs]# netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
      1 ESTABLISHED
      8 LISTEN
[root@nginx-kafka01 logs]# netstat -ant|awk '/ip:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -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

[root@nginx-kafka01 logs]# netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
      1 34.120.177.193
      1 0.0.0.0
[root@nginx-kafka01 logs]# netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n20
1 0.0.0.0

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

 tcpdump -i ens33  dst port 80 -c 1000 | awk -F"." '{print $1,$2,$3,$4}' | sort | uniq -c | sort -nr |head -20

4、根据端口列进程

netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1

[root@nginx-kafka01 logs]# netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1
6769

5、查找较多time_wait连接

netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
[root@nginx-kafka01 sc]# netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

6、找查较多的SYN连接

netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值