Switching the Wi-Fi network from terminal does not succeed (DHCP fails) I am trying to switch Wi-Fi network via the command line:

networksetup -setairportnetwork en0 corpnet01
networksetup -setairportnetwork en0 Home


where "Office" and "Home" are DHCP enabled networks. When I switch from  corpnet01 to Home, everything works fine. But the other way it doesn't.

When I do it via the UI all works OK:



but when I do it in the terminal, DHCP does not manage to get an IP address:



and networksetup says:

UM00160:~ taapega4$ networksetup -getinfo Wi-Fi
DHCP Configuration
IP address: 169.254.110.61
Subnet mask: 255.255.0.0
Router: (null)
Client ID:
IPv6: Automatic
IPv6 IP address: none
IPv6 Router: none
Wi-Fi ID: 8c:85:90:4f:41:77


What else is the UI doing that I am not doing by using the following command?

networksetup -setairportnetwork en0 corpnet01


I know that we require a certificate installed in the keychain to connect to corpnet01.
If you look at the usage for -setairportnetwork it implies that you have to include the password as an argument as well:

$ networksetup | grep setairportnetwork
Usage: networksetup -setairportnetwork &lt;device name&gt; &lt;network&gt; [password]


WiFi passwords are typically stored in Keychain so it's likely the case that the networking widget is pulling the password from Keychain and passing it as part of this operation behind the scenes.


How to find a Wi-Fi password on Mac


Example

Here I've looked in Keychain prior to get a rough idea of the item I'm attempting to retrieve via the CLI.



So we're looking for an item that contains "802.1X Password" for the "Kind". So we can use the security CLI tool to retrieve this like so:

$ security find-generic-password -D "802.1X Password" -g
keychain: "/Users/joeuser/Library/Keychains/login.keychain-db"
version: 512
class: "genp"
attributes:
    0x00000007 &lt;blob&gt;="SomeSSID Secure"
    0x00000008 &lt;blob&gt;=&lt;NULL&gt;
    "acct"&lt;blob&gt;="joeuser"
    "cdat"&lt;timedate&gt;=0x32303139303330343132323630355A00  "20190304122605Z\000"
    "crtr"&lt;uint32&gt;=&lt;NULL&gt;
    "cusi"&lt;sint32&gt;=&lt;NULL&gt;
    "desc"&lt;blob&gt;="802.1X Password"
    "gena"&lt;blob&gt;=&lt;NULL&gt;
    "icmt"&lt;blob&gt;=&lt;NULL&gt;
    "invi"&lt;sint32&gt;=&lt;NULL&gt;
    "mdat"&lt;timedate&gt;=0x32303139303330343132323630355A00  "20190304122605Z\000"
    "nega"&lt;sint32&gt;=&lt;NULL&gt;
    "prot"&lt;blob&gt;=&lt;NULL&gt;
    "scrp"&lt;sint32&gt;=&lt;NULL&gt;
    "svce"&lt;blob&gt;="com.apple.network.eap.user.item.wlan.ssid.SomeSSID Secure"
    "type"&lt;uint32&gt;=&lt;NULL&gt;
password: "securitypasswordssid123"


NOTE: When you run this command, you'll still be prompted for passwords to allow security CLI to access the Keychain. You can either do this 1 time, or allow it indefinitely.

The dialog will look like this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

References


How to find a Wi-Fi password on Mac
How to Find Wi-Fi Network Passwords from Command Line on Mac
802.1X Management on the command line