python查看网速_Python获取服务器网速

实时获取服务器网速的脚本

CentOS6版本

#!/usr/bin/python

import os, re, time

def get_net():

return re.search('eth0: (\d+)(\d+\s+){8}(\d+)', os.popen('cat /proc/net/dev').read())

first = get_net()

time.sleep(1)

second = get_net()

recive_speed = (int(second.group(1)) - int(first.group(1))) / 1024

send_speed = (int(second.group(3)) - int(first.group(3))) / 1024

recive_company = 'K/s'

send_company = 'K/s'

if send_speed > 1024:

send_speed = send_speed / 1024

send_company = 'M/s'

print('Recive : ' + str(recive_speed) + recive_company)

print('Send : ' + str(send_speed) + send_company)

CentOS7版本

#!/usr/bin/python

import os, re, time

def get_net():

return re.findall('\d+', os.popen('cat /proc/net/dev | grep ens33').read())

first = get_net()

time.sleep(1)

second = get_net()

recive_speed = (int(second[1]) - int(first[1])) / 1024

send_speed = (int(second[9]) - int(first[9])) / 1024

recive_company = 'K/s'

send_company = 'K/s'

if send_speed > 1024:

send_speed = send_speed / 1024

send_company = 'M/s'

if recive_speed > 1024:

recive_speed = recive_speed / 1024

recive_company = 'M/s'

print('Recive : ' + str(recive_speed) + recive_company)

print('Send : ' + str(send_speed) + send_company)

使用办法:

将此脚本保存到 /usr/local/bin/ 目录下,命名为 speed

添加可执行权限:chmod +x speed

watch -n 1 speed

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值