以编程方式连接到Mac上的wifi网络

时间:2017-07-17 17:45:30

标签: macos networking wifi

我试图在Mac上以编程方式连接到特定的wifi网络。我找到了this document

使用' airport'没有为我工作,所以我使用命令列出界面:

$ /usr/sbin/networksetup -listnetworkserviceorder
An asterisk (*) denotes that a network service is disabled.
(1) Bluetooth DUN
(Hardware Port: Bluetooth DUN, Device: Bluetooth-Modem)

(2) USB Ethernet
(Hardware Port: USB Ethernet, Device: en3)

(3) Display Ethernet
(Hardware Port: Display Ethernet, Device: en5)

(4) Display FireWire
(Hardware Port: Display FireWire, Device: fw0)

(5) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

(6) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en4)

(7) Thunderbolt Bridge
(Hardware Port: Thunderbolt Bridge, Device: bridge0)

然后我跑了:

$ /usr/sbin/networksetup -setairportnetwork en0 Internet

Failed to join network Internet.
Error: -3905  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3905.)Failed to join network Internet.
Error: -3905  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3905.)Failed to join network Internet.
Error: -3905  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3905.)Failed to join network Internet.
Error: -3905  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3905.)Failed to join network Internet.
Error: -3905  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3905.)Failed to join network Internet.
Error: -3905  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3905.)Failed to join network Internet.
Error: 82  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error 82.)Failed to join network Internet.
Error: -3903  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3903.)Failed to join network Internet.
Error: -3903  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3903.)Failed to join network Internet.
Error: -3903  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3903.)Failed to join network Internet.
Error: -3903  The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3903.)

我可以看到托盘上的wifi图标"处理"模式很长一段时间。

手动点击相同的托盘图标并点击网络即可成功连接到网络。

如何在Mac上以编程方式连接到wifi网络?

1 个答案:

答案 0 :(得分:0)

就像@Mark Setchell所说的那样,看来密码必须作为命令行参数提供。

/usr/sbin/networksetup -setairportnetwork en0 <my-essid> PASSWORD

Failed to join network Internet的错误消息似乎非常笼统,可以指示缺少参数或更具体的原因,例如“密码错误”等。

上面的命令行似乎也不适用于WPA2-EAP。