gpt4 book ai didi

python - 如何检查ppp连接是否繁忙

转载 作者:太空宇宙 更新时间:2023-11-04 05:40:28 25 4
gpt4 key购买 nike

要查看有关 ppp 连接的一些详细信息,可以运行以下命令:

$ ifconfig ppp
ppp0 Link encap:Point-to-Point Protocol
inet addr:197.108.58.82 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:479 errors:0 dropped:0 overruns:0 frame:0
TX packets:479 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:68118 (68.1 KB) TX bytes:32771 (32.7 KB)

为了检查是否有任何数据传输,可以重新运行该命令,同时注意RX数据包TX数据包以查看是否有变化(也许有更好的方法?)。

无论如何,我可以用Python做同样的事情,但是它很麻烦(使用子进程,然后解析输出),所以我想知道是否有更好的方法。我很想用 netifaces为此,但它提供了更多有限的信息:

$ python -c "import netifaces; print netifaces.ifaddresses('ppp0')"
{2: [{'peer': '10.64.64.64', 'netmask': '255.255.255.255', 'addr': '197.108.58.82'}]}

最佳答案

您可以在 /proc 中查找数据,如果这比调用 ifconfig 更容易。

def GetPacketCount(dev_name):
'''Return (received_packets, transmitted_packets) for network device dev_name'''
with open('/proc/net/dev') as fp:
for line in fp:
line = line.split()
if line[0].startswith(dev_name):
return int(line[2]), int(line[10])

if __name__ == '__main__':
import sys
print GetPacketCount(sys.argv[1])

引用:https://www.kernel.org/doc/Documentation/filesystems/proc.txt

关于python - 如何检查ppp连接是否繁忙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19551194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com