zoukankan      html  css  js  c++  java
  • python客户端网络延时检测

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
    import subprocess
    import re
    import time
    
    class LinkState(object):
        def __init__(self):
            self.ipaddr()
    
        # 获取当前时间
        def get_time(self):
            return str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
    
        #获取IP
        def ipaddr(self):
            comand="netstat -ntp | awk -F '[ :]+' '{if($9 == "'"19200/sshd"'" && $6 != "'"127.0.0.1"'"){print $6}}'"
            res=subprocess.Popen(comand,shell=True,stdout=subprocess.PIPE)
            IP=res.stdout.read().decode('utf-8')
            if IP:
                self.getLinkState(100,IP)
    
        # 获取网络延时
        def getLinkState(self,count,ip):
            #运行ping程序
            p = subprocess.Popen(("ping -c %s %s" %(count,ip)),
                 stdin = subprocess.PIPE,
                 stdout = subprocess.PIPE,
                 stderr = subprocess.PIPE,
                 shell = True)
    
            #得到ping的结果
            out = p.stdout.read().decode('utf-8')
    
            #找出丢包率,这里通过‘%’匹配
            regex = re.compile(r'w*%w*')
            packetLossRateList = regex.findall(out)
            self.packetLossRate = packetLossRateList[0]
    
            #找出找出延时最大值,最小值,平均值
            regex = re.compile(r'd+.d+/')
            timeList = regex.findall(out)
            self.minTime = timeList[-3].replace('/','ms')
            self.maxTime = timeList[-1].replace('/','ms')
            self.averageTime = timeList[-2].replace('/','ms')
            result = {'Loss':self.packetLossRate,'min':self.minTime,'max':self.maxTime,'avg':self.averageTime}
            print(result)
    
    if __name__ == '__main__':
        LinkState()
  • 相关阅读:
    springboot启动后执行某些动作
    Virtualbox的nat网络
    xshell6
    day01 K8S
    Nginx的日志文件切割
    virtualbox磁盘空间大小调整
    装修柜子木台面
    mybatis 批量in 多个字段写法
    jenkins shell常用配置
    activiti工作流引擎数据库表结构
  • 原文地址:https://www.cnblogs.com/xwupiaomiao/p/11928149.html
Copyright © 2011-2022 走看看