【问题标题】:"Status" field in /proc/net/wireless/proc/net/wireless 中的“状态”字段
【发布时间】:2024-04-13 00:35:01
【问题描述】:

我尝试使用 bash 脚本检查是否有任何无线接口已启动。我想我可以通过检查 /proc/net/wireless 中每个接口的 Status 字段来做到这一点。但是,我试图查找对该字段中可能值的参考以及它们的含义,但似乎没有任何结果。有人知道吗?这是解决这个问题的理想方法吗?

【问题讨论】:

  • 我对无线接口不太熟悉,但是你不能用ifconfigiwconfig和grep UP什么的吗?

标签: linux bash networking


【解决方案1】:

您将需要检查每个接口的operstate 以判断它是否是;向上、向下或未知。这是使用GNU awk的一种方式:

awk '{ split(FILENAME, array, "/"); print array[5] ": " $1 }' $(find /sys/class/net/*/operstate ! -type d)

在我的系统上,以下是一些结果:

eth0: up
lo: unknown
vboxnet0: down
wlan0: up

要仅检查无线接口,您需要在每个接口下检查一个名为“无线”的文件夹。这是使用GNU awk 的一种方法。

awk -F "/" 'FNR==NR { wire[$5]++; next } { split(FILENAME, state, "/"); if (state[5] in wire && $1 == "up") print state[5] }' <(find /sys/class/net/*/wireless -type d) $(find /sys/class/net/*/operstate ! -type d)

结果:

wlan0

伪代码:

1. Get the directory names of the wireless devices as the 1st argument
2. Split these names on the "/" delimiter
3. Add the 5th column (the name of the wireless device) to an array called 'wire'
4. Now read in the operstates of all network interfaces as the 2nd argument
5. Split the interface filenames on the "/" delimiter to an array called 'state'
6. If the interface is a wireless interface (i.e. if it's in the array called
   wire) and its operstate is "up", print it.

【讨论】:

  • 谢谢。这似乎是朝着正确方向迈出的一步。但是,仅返回无线接口的第二个和第三个 awk 语句不取决于所有以“wlan”开头的无线接口的名称吗?很可能在很多情况下都会出现这种情况,对吧?
  • @timtran:我删除了第二个和第三个 awk 语句,因为无线接口可能用奇怪的字符命名。在我的测试中,我发现每个无线接口都应该包含一个名为“无线”的目录。上面编辑过的代码会检查这一点,并且仅在它们“启动”时才返回无线接口名称。请看伪代码。 HTH。
最近更新 更多