如何在Linux上编程获取网络接口速度?

3

我想了解网络连接信息中显示的速度,例如100Mb/s、10Mb/s,而不是可用带宽。提前感谢!

2个回答

4
如果您想在C代码中实现这个功能,请查阅mii-tool.c

信息是通过查询适当的ioctl函数完成的,而mii-tool.c提供了示例。 - rasjani

2
http://linuxhelp.blogspot.com/2005/10/find-speed-of-your-ethernet-card-in.html上找到
以编程方式读取此信息并解析它,查找100baseTx或类似的内容
# mii-tool -v eth0
eth0: negotiated 100baseTx-FD flow-control, link ok
  product info: vendor 00:00:20, model 32 rev 1
  basic mode:   autonegotiation enabled
  basic status: autonegotiation complete, link ok
  capabilities: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
  advertising:  100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control
  link partner: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control

同时,在同一篇文章中,您可以使用这个方法并查找“速度:”一行。
#ethtool eth0

Settings for eth0:
  Supported ports: [ TP MII ]
  Supported link modes:   10baseT/Half 10baseT/Full
                          100baseT/Half 100baseT/Full
  Supports auto-negotiation: Yes
  Advertised link modes:  10baseT/Half 10baseT/Full
                          100baseT/Half 100baseT/Full
  Advertised auto-negotiation: Yes
  Speed: 100Mb/s
  Duplex: Full
  Port: MII
  PHYAD: 32
  Transceiver: internal
  Auto-negotiation: on
  Supports Wake-on: pumbg
  Wake-on: p
  Current message level: 0x00000007 (7)
  Link detected: yes

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