社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  linux

请求分析

Py站长 • 4 年前 • 798 次点击  

对连接的IP按连接数量进行排序

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

查看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

查看80端口连接数最多的20个IP netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20

查找较多time_wait连接 netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20 查找较多的SYN连接 netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more

查看当前并发访问数: netstat -an | grep ESTABLISHED | wc -l

查看所有连接请求 netstat -tn 2>/dev/null 但是只要established的,则grep "ESTABLISHED" netstat -tn | grep ESTABLISHED 2>/dev/null

查看访问某一ip的所有外部连接IP(数量从多到少)

netstat -nt | grep 121.41.30.149:80 | awk '{print $5}' | awk -F: '{print ($1>$4?$1:$4)}' | sort | uniq -c | sort -nr | head

根据端口查找进程 netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1

下面未验证 防范DDOS×××脚本

防止SYN××× 轻量级预防

iptables -N syn-flood
iptables -A INPUT -p tcp –syn -j syn-flood
iptables -I syn-flood -p tcp -m limit –limit 3/s –limit-burst 6 -j RETURN
iptables -A syn-flood -j REJECT

防止DOS太多连接进来,可以允许外网网卡每个IP最多15个初始连接,超过的丢弃

iptables -A INPUT -i eth0 -p tcp –syn -m connlimit –connlimit-above 15 -j DROP
iptables -A INPUT -p tcp -m state –state ESTABLISHED,RELATED -j ACCEPT

用Iptables抵御DDOS (参数与上相同)

iptables -A INPUT  -p tcp --syn -m limit --limit 12/s --limit-burst 24 -j ACCEPT
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT

根据nginx的访问日志判断 查看访问记录 1.从1000行开始到3000 cat access.log |head -n 3000|tail -n 1000 2.从1000行开始,显示200行 cat access.log |tail -n +1000 |head -n 200 3. 通过查询日志记录进行分析(如果没有单独配置,access.log一般放在nginx/logs下)

awk '{print $1}' 日志地址 | sort | uniq -c | sort -n -k 1 -r | head -n 100
tail -n 1000:显示最后1000行
tail -n +1000:从1000行开始显示,显示1000行以后的
head -n 1000:显示前面1000行

1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l

2.统计访问URL统计PV awk '{print $7}' access.log|wc -l

3.查询访问最频繁的URL awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more

4.查询访问最频繁的IP awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more

5.根据时间段统计查看日志 cat access.log| sed -n '/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p'|more

6.通过日志查看含有send的url,统计ip地址的总连接数 cat access.log | grep "send" | awk '{print $1}' | sort | uniq -c | sort -nr

7.通过日志查看当天访问次数最多的时间段 awk '{print $4}' access.log | grep "24/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head

8.通过日志查看当天指定ip访问次数过的url和访问次数 cat access.log | grep "222.132.90.94" | awk '{print $7}' | sort | uniq -c | sort -nr

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54726
 
798 次点击