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

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 查看IP访问量的shell脚本汇总

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

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

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

相关文章
|
2天前
|
监控 Unix Shell
shell脚本编程学习
shell脚本编程
22 12
|
5天前
|
Shell
shell脚本变量 $name ${name}啥区别
shell脚本变量 $name ${name}啥区别
|
8天前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
25 2
|
29天前
|
Shell
Shell脚本有哪些基本语法?
【9月更文挑战第4天】
43 17
|
29天前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
36 12
|
27天前
|
网络协议 关系型数据库 MySQL
Shell 脚本案例
Shell 脚本案例
36 8
|
28天前
|
Shell Linux 开发工具
linux shell 脚本调试技巧
【9月更文挑战第3天】在Linux中调试shell脚本可采用多种技巧:使用`-x`选项显示每行命令及变量扩展情况;通过`read`或`trap`设置断点;利用`echo`检查变量值,`set`显示所有变量;检查退出状态码 `$?` 进行错误处理;使用`bashdb`等调试工具实现更复杂调试功能。
|
2月前
|
运维 监控 Shell
自动化运维之宝:编写高效的Shell脚本
【8月更文挑战第31天】在运维的世界里,Shell脚本是一把瑞士军刀,它让日常任务变得简单而高效。本文将通过浅显易懂的语言和实际案例,带你领略Shell脚本的魅力,并教你如何打造属于自己的自动化工具箱。无论你是初学者还是资深运维,这篇文章都将为你打开一扇窗,让你看到不一样的风景。让我们一起探索Shell脚本的世界吧!
|
2月前
|
存储 Shell 数据安全/隐私保护
minio一键安装脚本分享(shell和python)
minio一键安装脚本分享(shell和python)
46 0
|
2月前
|
关系型数据库 Shell 数据库
postgres14一键安装脚本分享(shell和python)
postgres14一键安装脚本分享(shell和python)
36 0