我试图检查是否有任何无线接口使用bash脚本。我想我可以通过检查/proc/net/wireless中每个接口的status字段来实现这一点。然而,我试着去寻找这个领域中可能存在的价值以及它们的含义,但似乎什么都没有找到。有人知道吗?这是解决这个问题的理想方法吗?

最佳答案

您需要检查每个接口的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

要仅检查无线接口,需要在每个接口下检查名为“wireless”的文件夹。这里有一种使用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.

07-26 09:29