zoukankan      html  css  js  c++  java
  • 监控网卡流量脚本(Python)

    #!/usr/bin/env python
    # coding: utf-8
    # author: Xiao Guaishou

    try:
        import psutil
    except ImportError:
        print('Error: psutil module not found!')
        exit()


    def get_key():

        key_info = psutil.net_io_counters(pernic=True).keys()

        recv = {}
        sent = {}

        for key in key_info:
            recv.setdefault(key, psutil.net_io_counters(pernic=True).get(key).bytes_recv)
            sent.setdefault(key, psutil.net_io_counters(pernic=True).get(key).bytes_sent)

        return key_info, recv, sent


    def get_rate(func):

        import time

        key_info, old_recv, old_sent = func()

        time.sleep(1)

        key_info, now_recv, now_sent = func()

        net_in = {}
        net_out = {}

        for key in key_info:
            net_in.setdefault(key, (now_recv.get(key) - old_recv.get(key)) / 1024)
            net_out.setdefault(key, (now_sent.get(key) - old_sent.get(key)) / 1024)

        return key_info, net_in, net_out


    while 1:
        try:
             key_info, net_in, net_out = get_rate(get_key)

             for key in key_info:
                 print('%s Input: %-5sKB/s Output: %-5sKB/s ' % (key, net_in.get(key), net_out.get(key)))
        except KeyboardInterrupt:
            exit()

    # End

  • 相关阅读:
    apiCode/1/1.1/1.1.1
    ZOJ 3195 Design the city LCA转RMQ
    IOS学习之路十二(UITableView下拉刷新页面)
    ASP.NET 缓存技术分析
    电信支撑系统
    android提权
    awk
    linux高效shell命令总结
    C关键字typedef及argc,argv,env参数含义
    2013年6月编程语言排行榜,C语言位据第一位
  • 原文地址:https://www.cnblogs.com/yueminghai/p/6626948.html
Copyright © 2011-2022 走看看