Search code examples
macosnetwork-programmingwifi

programmatically connect to a wifi network on a Mac


I'm trying to connect to a specific wifi network programmatically on a Mac. I found this document

Using 'airport' didn't work for me, so I used the command to list the interface:

$ /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)

Then I ran:

$ /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.)

I could see the wifi icon on the tray in "processing" mode for a long time.

Manually clicking on the same tray icon and clicking on the network does succeed in connecting to the network.

How do I connect to a wifi network programmatically on a Mac?


Solution

  • Like @Mark Setchell stated, it appears that the password must be provided as a command-line argument.

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

    The error message of Failed to join network Internet seems very general, and could instead indicate a missing argument or a more specific reason like "bad passphrase", etc.

    The command line above also does not seem to work for WPA2-EAP.