在寻找获取各种WiFi信息的方法时,我找到了/sys/class/net/wlan7/speed文件。问题是,即使授予了所有读取权限,我也无法读取它。
$ ls -l /sys/class/net/wlan7/speed
-r--r--r-- 1 root root 4096 3? 24 15:08 /sys/class/net/wlan7/speed
$ sudo cat /sys/class/net/wlan7/speed
[sudo] password for xieerqi:
cat: /sys/class/net/wlan7/speed: Invalid argument
Run Code Online (Sandbox Code Playgroud)
我可以统计文件,我可以列出ls,但是每次我尝试读取它(使用hexdump、使用cat、使用二进制读取模式的 Python 等)时,它都会显示无效参数错误。
strace 表明此时显然发生错误:
read(3, 0x7f6ad6b44000, 131072) = -1 EINVAL (Invalid argument)
write(2, "cat: ", 5cat: ) = 5
write(2, "/sys/class/net/wlan7/speed", 26/sys/class/net/wlan7/speed) = 26
open("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 4
Run Code Online (Sandbox Code Playgroud)
(完整跟踪粘贴的链接:http : //paste.ubuntu.com/24252504/)
相比之下,以太网接口的相应文件有效:
$ cat /sys/class/net/eth3/speed
10
Run Code Online (Sandbox Code Playgroud)
所以基本上我的问题是:我如何读取这个文件?
操作系统:Ubuntu 16.04 LTS,内核 4.4.0-67-generic,AR9565 WiFi 适配器
该文件/sys/class/net/<iface>/speed仅对支持ethtool get_settings 的设备有效方法的方法主要适用于以太网适配器。
要获得 WiFi 设备的速度,您可以使用iwconfig或iwlist。
iwconfig wlan7
iwlist wlan7 rate
Run Code Online (Sandbox Code Playgroud)
从内核文档:
What: /sys/class/net/<iface>/speed
...
Description:
Indicates the interface latest or current speed value. Value is
an integer representing the link speed in Mbits/sec.
Note: this attribute is only valid for interfaces that implement
the ethtool get_settings method (mostly Ethernet ).
Run Code Online (Sandbox Code Playgroud)