获取 Wi-Fi 接口设备名称

Mat*_*ite 32 linux command-line networking wifi network-interface

之前关于查找网络设备名称的问题类似,我想获得一个(可靠的)设备名称列表,但仅适用于 Wi-Fi 设备。根据您的命名结构,它看起来如下所示:

wlan0
wlan1
Run Code Online (Sandbox Code Playgroud)

或者

wlp5s0
wlp5s1
Run Code Online (Sandbox Code Playgroud)

don*_*sti 32

在 linux 上,您有iw(显示/操作无线设备及其配置)并且与dev命令一起使用时:

Commands:
    dev
        List all network interfaces for wireless hardware.
Run Code Online (Sandbox Code Playgroud)

那是

iw dev
Run Code Online (Sandbox Code Playgroud)

你会得到类似的东西:

phy#0
    Interface wlan0
        ifindex 3
        wdev 0x1
        addr 00:12:32:e4:18:24
        type managed
phy#1
    Interface wlan1
        ifindex 4
        wdev 0x2
        addr 00:12:22:c6:b2:0a
        type managed
Run Code Online (Sandbox Code Playgroud)

如果您只想提取接口名称,您可以随时处理输出,例如

iw dev | awk '$1=="Interface"{print $2}'
Run Code Online (Sandbox Code Playgroud)

请记住,帮助页面明确指出:

Do NOT screenscrape this tool, we don't consider its output stable.
Run Code Online (Sandbox Code Playgroud)


Mat*_*ite 12

至少在 Ubuntu 上,有一个/proc/net/wireless文件包含有关 Wi-Fi 接口的详细信息。哪些输出适合我:

$ cat /proc/net/wireless
Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE
 face | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | 22
wlp5s0: 0000   36.  -74.  -256        0      0      0     16  33004        0
Run Code Online (Sandbox Code Playgroud)

它有点乱,但设备名称在那里。

只获取接口名称:

cat /proc/net/wireless | perl -ne '/(\w+):/ && print $1'
Run Code Online (Sandbox Code Playgroud)

perl代码打印冒号前的单词字符串。

  • 这仅显示连接的网络。 (5认同)

Jos*_*osh 9

如果您在 /sys 安装了 sysfs,则以下命令有效:

$ find /sys/class/net -follow -maxdepth 2 -name wireless | cut -d / -f 5
wlan0
$ find /sys/class/net -follow -maxdepth 2 -name phy80211 | cut -d / -f 5
wlan0
Run Code Online (Sandbox Code Playgroud)

或者,没有find

$ find /sys/class/net -follow -maxdepth 2 -name wireless | cut -d / -f 5
wlan0
$ find /sys/class/net -follow -maxdepth 2 -name phy80211 | cut -d / -f 5
wlan0
Run Code Online (Sandbox Code Playgroud)

第一查找所有设备在/sys/class/net同一个wireless目录中(其可以是多只的WiFi设备)和其是802.11兼容的第二设备的发现

在内核 4.4 上测试


D. *_*ble 7

基于Josh 的回答,我将使用 shell glob 来识别包含/sys/class/net目录的wireless目录,并cut获取设备名称:

# find the directories
$ printf '%s\n' /sys/class/net/*/wireless # substitute with phy80211 if desired
/sys/class/net/wlp4s0/wireless
# filter out the "device" part
$ printf '%s\n' /sys/class/net/*/wireless | cut -d/ -f5
wlp4s0
Run Code Online (Sandbox Code Playgroud)


小智 6

在 Android 4、Android 7.1 和 Android 9 和 ArchLinux 上测试的通用方式(非 root)。

ls /sys/class/ieee80211/*/device/net/
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方案。您的解决方案也适用于 SailfishOS Linux 和某些 Ubuntu。 (3认同)