微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想通过python代码来计算wlan1接口上的总networkingstream量。 Uptil现在我尝试了ethtool , iftop , ifstat , nethogs但大多数这些工具显示ncurses接口(文本基础用户界面)。

我尝试了这样的事情

import subprocess nw_usage = subprocess.Popen(['ifstat','-i','wlan1'])

但它不给我networking使用价值。

我无法弄清楚如何从ncurses接口获取单个variables的networking使用情况值。 (而且我感觉到会有一些更好的方法来计算networking使用)

Oracle Linux前端

在Qt-Creator SDK上以root身份运行代码

在64位Linux中构build32位可执行文件时,我得到crt1.o:即使使用-m32标志也没有这样的文件

dquote>在linux shell中执行一个程序的结果

适用于Linux的Ada编译器

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

谢谢

cdc_acm:未能设置dtr / rts – 无法与usb cdc设备通信

PHP / PAM更改用户密码

Linux AMD64从复制程序集调用C库函数

PHP升级到版本7后,为什么我不能使用mongodb驱动程序?

如何卸载Python2.6

我知道这个问题是几个星期的老,但也许这个答案仍然可以帮助:)

您可以从/ 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]))

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

ifconfig; sleep 10; ifconfig;

然后,只需在相关接口上减去“TX字节”(在发送的情况下),再以10(休眠时间)为单位,以每秒B字节为单位进行粗略估计。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐