【问题标题】:programmatically connect to a wifi network on a Mac以编程方式连接到 Mac 上的 wifi 网络
【发布时间】:2017-07-17 17:45:30
【问题描述】:

我正在尝试在 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 网络?

【问题讨论】:

  • 你确定需要/usr/sbin/networksetup -setairportnetwork en0 Internet PASSWORD
  • @MarkSetchell 就是这样。由于在通过 UI 选择网络时不需要重新输入已知网络的密码,因此我没想到必须提供密码明文作为命令行参数。您可以添加您的评论作为答案吗?

标签: macos networking wifi


【解决方案1】:

就像@Mark Setchell 所说,密码似乎必须作为命令行参数提供。

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

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

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

【讨论】:

猜你喜欢
  • 2013-04-30
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多