python编写linux系统dos检测脚本

该脚本功能

1、检测IP连接数;

2、1分钟内的cpu静态负载情况

3、网络连接数量

4、CPU空闲率

5、等待队列长度插差值

#!bin/python
import os
#
def get_net():
    '''网络是否异常>500'''
    cmd="netstat -ant | wc -l "
    res=os.popen(cmd).read()
    rest=(res.strip())
    return rest
#         
def get_uptime():
    '''uptime 1min'''
    cmd = " uptime |awk -F ',' '{print $3}' | awk '{print $3}' "
    res = os.popen(cmd).read()
    res = (res.strip())
    return res
#    
def get_top():
    try:
        cmd = " top -n 1 | grep %Cpu |awk -F ',' '{print $4}' | awk -F 'i' '{print $1}' "
        res = os.popen(cmd).read()
        res = res.strip().split(' ')
        a = float(res[1])
    except:
        a=100.0
    return a
#
def get_queue(n):
    '''ss -lnt | grep : 99 Recv_Q的值'''
    cmd="ss -lnt|grep :8888| awk '{print $%s}' "%n
    res=os.popen(cmd).read()
    res = (res.strip())
    return res
#
#
def get_que_diff():
    #print(worning_dict)
    '''采集队列长度的差值:(Send_Q  -  Recv_Q)'''
    after=int(get_queue(3))
    befor = int(get_queue(3))
    res=after-befor
    return res
#
#
def get_net_recv():
    cmd="netstat -anpt | awk '{print $4,$6}' | sort | uniq -c "
    res = os.popen(cmd).readlines()
    try:
     worning_dict={}
     for i in res:
       make_lis = i.strip().split(' ')
       if int(make_lis[0]) > 50:
           worning_dict.setdefault(f"IP:{make_lis[1]}",f"连接数:{make_lis[0]}")
     #print(worning_dict)
    except:
     pass
    return worning_dict
print("相通IP连接数量过高:",get_net_recv())
print("1分钟内CPU静态负载情况:",get_uptime())
print("网络连接数量:",get_net())
print("CPU空闲率:",get_top())
print("等待队列长度差值:",get_que_diff())
worning_dict=get_net_recv()  #获取syn的连接数量
for k,v in worning_dict.items():
    print(k,v)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值