如何在Linux中以编程方式查找网络使用情况

3
我将尝试通过Python代码计算wlan1接口的总网络流量。到目前为止,我已经尝试了ethtool、iftop、ifstat和nethogs等工具,但大多数这些工具都显示ncurses界面(文本基础UI)。
我尝试了以下内容:
import subprocess
nw_usage = subprocess.Popen(['ifstat', '-i', 'wlan1'])

但是它没有给我网络使用值。

我无法从ncurses界面中获得单个变量的网络使用值。(而且我感觉有更好的方法来计算网络使用情况)

任何帮助或指导将是一个巨大的帮助。

谢谢


你考虑过使用 tshark 吗?它是 Wireshark 的命令行版本。 - inspectorG4dget
定义网络使用。您想要了解什么?网络接口的流量速度吗? - denisvm
我需要在给定时间获取网络接口的传输速率。例如,x=somefunction(wlan1) 应该给我 x Kbps/Mbps,以便我可以将其与该接口的最大传输速率(即 56Mbps 连接速度)进行比较。 - Waqas
inspectorG4dget 我刚试了 tshark,但我不需要数据包...我需要传输速率。 - Waqas
2个回答

4

我知道这个问题已经几周了,但也许这个答案仍然有帮助 :)

您可以从/proc/net/dev读取设备统计信息。在一个时间间隔内读取传输/接收字节并计算差异。这是我拼凑出来的一些简单的Python脚本:

import re
import time


# A regular expression which separates the interesting fields and saves them in named groups
regexp = r"""
  \s*                     # a interface line  starts with none, one or more whitespaces
  (?P<interface>\w+):\s+  # the name of the interface followed by a colon and spaces
  (?P<rx_bytes>\d+)\s+    # the number of received bytes and one or more whitespaces
  (?P<rx_packets>\d+)\s+  # the number of received packets and one or more whitespaces
  (?P<rx_errors>\d+)\s+   # the number of receive errors and one or more whitespaces
  (?P<rx_drop>\d+)\s+      # the number of dropped rx packets and ...
  (?P<rx_fifo>\d+)\s+      # rx fifo
  (?P<rx_frame>\d+)\s+     # rx frame
  (?P<rx_compr>\d+)\s+     # rx compressed
  (?P<rx_multicast>\d+)\s+ # rx multicast
  (?P<tx_bytes>\d+)\s+    # the number of transmitted bytes and one or more whitespaces
  (?P<tx_packets>\d+)\s+  # the number of transmitted packets and one or more whitespaces
  (?P<tx_errors>\d+)\s+   # the number of transmit errors and one or more whitespaces
  (?P<tx_drop>\d+)\s+      # the number of dropped tx packets and ...
  (?P<tx_fifo>\d+)\s+      # tx fifo
  (?P<tx_frame>\d+)\s+     # tx frame
  (?P<tx_compr>\d+)\s+     # tx compressed
  (?P<tx_multicast>\d+)\s* # tx multicast
"""


pattern = re.compile(regexp, re.VERBOSE)


def get_bytes(interface_name):
    '''returns tuple of (rx_bytes, tx_bytes) '''
    with open('/proc/net/dev', 'r') as f:
        a = f.readline()
        while(a):
            m = pattern.search(a)
            # the regexp matched
            # look for the needed interface and return the rx_bytes and tx_bytes
            if m:
                if m.group('interface') == interface_name:
                    return (m.group('rx_bytes'),m.group('tx_bytes'))
            a = f.readline()


while True:
    last_time  = time.time()
    last_bytes = get_bytes('wlan0')
    time.sleep(1)
    now_bytes = get_bytes('wlan0')
    print "rx: %s B/s, tx %s B/s" % (int(now_bytes[0]) - int(last_bytes[0]), int(now_bytes[1]) - int(last_bytes[1]))

太棒了,即使在Python 3中也能很好地工作。做得非常好! - X99
做得非常好!(Y) - abhilash_goyal

0

可能有更好的方法,但我曾经使用的一个丑陋的解决方法是从命令行估算网络速率:

ifconfig; sleep 10; ifconfig;

然后只需从相关接口中减去“TX字节”(在传输的情况下),并除以10(睡眠时间)以得到每秒字节的粗略估计。


我需要更快的结果,接近实时一些。 - Waqas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接