简体   繁体   English

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

[英]Get wireless SSID through shell script on Mac OS X

有没有办法在 Mac OS X 上通过 shell 脚本获取当前无线网络的 SSID?

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 $2}'

To retrieve SSID names that might have colons as well as spaces:要检索可能包含冒号和空格的 SSID 名称:

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

Where isn't there 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 $2}' | xargs networksetup -getairportnetwork

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)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM