macos 在 Mac OS X 上通过 shell 脚本获取无线 SSID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4481005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:27:56  来源:igfitidea点击:

Get wireless SSID through shell script on Mac OS X

macosshell

提问by Mark Szymanski

Is there any way to get the SSID of the current wireless network through a shell script on Mac OS X?

有什么办法可以通过 Mac OS X 上的 shell 脚本获取当前无线网络的 SSID?

回答by Chetan

The command

命令

/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I

will give you details about your current wireless network connection.

将为您提供有关当前无线网络连接的详细信息。

To get specifically the SSID, use this command:

要具体获取 SSID,请使用以下命令:

/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F: '/ SSID/{print }'

回答by Sacrilicious

Where isn'tthere a wheel in need of re-inventing?

哪里没有需要重新发明的轮子?

networksetup -getairportnetwork en1 | cut -c 25-

is what you'd use on 10.6, 10.7 changed the "Hardware Port" name from "Airport" to "Wi-Fi", and therefore you'd cut off one less letter,

是您在 10.6 上使用的,10.7 将“硬件端口”名称从“机场”更改为“Wi-Fi”,因此您少了一个字母,

aru$ networksetup -getairportnetwork en1 | cut -c 24-
Yorimichi

In case the device is named something other than en1, one needs to first get the correct device name, than the corresponding SSID:

如果设备的名称不是en1,则需要首先获得正确的设备名称,而不是相应的 SSID:

networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print }' | xargs networksetup -getairportnetwork

回答by Johnsyweb

The following has been tested on OS X and prints the SSID without any hard-coded column widths:

以下已在 OS X 上进行测试,并在没有任何硬编码列宽的情况下打印 SSID:

system_profiler SPAirPortDataType | awk -F':' '/Current Network Information:/ {
    getline
    sub(/^ */, "")
    sub(/:$/, "")
    print
}'

Essentially, this takes the output of system_profiler SPAirPortDataType, and prints the line after "Current Network Information:" trimming leading whitespace and the trailing colon (since SSIDs can contain :s).

本质上,这需要输出system_profiler SPAirPortDataType, 并在Current Network Information:修剪前导空格和尾随冒号后打印“ ”行(因为 SSID 可以包含:s)。