linux查看每秒的网络流量

import os
import time
cmd = 'ifconfig | grep "RX bytes" | tail -1 | awk -F":" \'{print $2," ", $3}\' | awk \'{print $1 , " " , $6}\' > network_speed.temp'
os.system(cmd)
tx=0
rx=0
while True:
        file = open('network_speed.temp')
        for line in file:
                fields = line.strip().split()
        print time.asctime( time.localtime(time.time()) ), " " ,(int(fields[0]) - tx)/1024.0/1024.0, " ", (int(fields[1]) - rx)/1024.0/1024.0
        tx = int(fields[0])
        rx= int(fields[1])
        time.sleep(0.1)
        os.system(cmd)

 

posted @ 2019-06-19 20:30  友哥  阅读(792)  评论(0编辑  收藏  举报