一、前言
服务器有的时候会被人搞崩,cpu莫名飙升,为了查看哪些IP访问过于频繁,就可以使用netstat、awk等指令进行统计查看。
二、指令
对一些常用的指令总结如下:
1、常用指令
对连接的IP按连接数量进行排序:
Shell
netstat -ntu | awk ‘{print $5}‘ | cut -d: -f1 | sort | uniq -c | sort -n
1
netstat -ntu | awk ‘{print $5}‘ | cut -d: -f1 | sort | uniq -c | sort -n
查看TCP连接状态:
Shell
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
1
2
3
4
5
6
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
查看80端口连接数最多的20个IP:
Shell
netstat -anlp|grep 80|grep tcp|awk ‘{print $5}‘|awk -F: ‘{print $1}‘|sort|uniq -c|sort -nr|head -n20
1
netstat -anlp|grep 80|grep tcp|awk ‘{print $5}‘|awk -F: ‘{print $1}‘|sort|uniq -c|sort -nr|head -n20
查找较多time_wait连接:
Shell
netstat -n|grep TIME_WAIT|awk ‘{print $5}‘|sort|uniq -c|sort -rn|head -n20
1
netstat -n|grep TIME_WAIT|awk ‘{print $5}‘|sort|uniq -c|sort -rn|head -n20
查找较多的SYN连接:
Shell
netstat -an | grep SYN | awk ‘{print $5}‘ | awk -F: ‘{print $1}‘ | sort | uniq -c | sort -nr | more
1
netstat -an | grep SYN | awk ‘{print $5}‘ | awk -F: ‘{print $1}‘ | sort | uniq -c | sort -nr | more
查看当前并发访问数:
Shell
netstat -an | grep ESTABLISHED | wc -l
1
netstat -an | grep ESTABLISHED | wc -l
查看所有连接请求:
Shell
netstat -tn 2>/dev/null
1
netstat -tn 2>/dev/null
但是只要established的,则grep "ESTABLISHED":
Shell
netstat -tn | grep ESTABLISHED 2>/dev/null
1
netstat -tn | grep ESTABLISHED 2>/dev/null
查看访问某一ip的所有外部连接IP(数量从多到少):
Shell
netstat -nt | grep 121.41.30.149:80 | awk ‘{print $5}‘ | awk -F: ‘{print ($1>$4?$1:$4)}‘ | sort | uniq -c | sort -nr | head
1
netstat -nt | grep 121.41.30.149:80 | awk ‘{print $5}‘ | awk -F: ‘{print ($1>$4?$1:$4)}‘ | sort | uniq -c | sort -nr | head
根据端口查找进程:
Shell
netstat -ntlp | grep 80 | awk ‘{print $7}‘ | cut -d/ -f1
1
netstat -ntlp | grep 80 | awk ‘{print $7}‘ | cut -d/ -f1
2、根据nginx的访问日志判断
在网站部署的目录下,会有个wwwlogs文件夹用于存放一些日志文件。我们可以根据其中的access.log文件查看一些访问记录。
查看访问记录,从1000行开始到3000:
Shell
cat access.log |head -n 3000|tail -n 1000
1
cat access.log |head -n 3000|tail -n 1000
查看访问记录,从1000行开始,显示200行:
Shell
cat access.log |tail -n +1000 |head -n 200
1
cat access.log |tail -n +1000 |head -n 200
根据访问IP统计UV:
Shell
awk ‘{print $1}‘ access.log|sort | uniq -c |wc -l
1
awk ‘{print $1}‘ access.log|sort | uniq -c |wc -l
统计访问URL统计PV:
Shell
awk ‘{print $7}‘ access.log|wc -l
1
awk ‘{print $7}‘ access.log|wc -l
查询访问最频繁的URL:
Shell
awk ‘{print $7}‘ access.log|sort | uniq -c |sort -n -k 1 -r|more
1
awk ‘{print $7}‘ access.log|sort | uniq -c |sort -n -k 1 -r|more
查询访问最频繁的IP:
Shell
awk ‘{print $1}‘ access.log|sort | uniq -c |sort -n -k 1 -r|more
1
awk ‘{print $1}‘ access.log|sort | uniq -c |sort -n -k 1 -r|more
通过日志查看含有send的url,统计ip地址的总连接数:
Shell
cat access.log | grep "send" | awk ‘{print $1}‘ | sort | uniq -c | sort -nr
1
cat access.log | grep "send" | awk ‘{print $1}‘ | sort | uniq -c | sort -nr
通过日志查看当天指定ip访问次数过的url和访问次数:
Shell
cat access.log | grep "222.132.90.94" | awk ‘{print $7}‘ | sort | uniq -c | sort -nr
1
cat access.log | grep "222.132.90.94" | awk ‘{print $7}‘ | sort | uniq -c | sort -nr
原文地址:https://blog.51cto.com/982439641/2468349