5

I'm looking for a way to disconnect from my current WIFI network via CLI. There is a way to do that, but it's a GUI way.

Any ideas how to accomplish that?

Thanks!

a busy cat

Note: I don't want to disable Wi-Fi because I need Airdrop to be function so I just want to disconnect it from the existing network.

EDIT: Why do I want this?

At work we connect out MBPs to Displays w/ ethernet attached. Also we heavily use AirDrop functionality. The aim is to write a script, that automatically disconnects from WIFI networks, when ethernet cable is plugged in, so that we can still use other WIFI features like AirDrop & Apple Watch unlocking.

7

Use networksetup.

There are several ways to disconnect from a Wi-Fi network via Terminal:

  • Disable the Wi-Fi service. This will require root privileges. The radio will remain on, but it won't connect to anything.

    $ sudo networksetup -setnetworkserviceenabled Wi-Fi off

  • Turn off the airport power (this is like clicking the (GUI) Wi-Fi icon and selecting "turn off Wi-Fi". You don't need sudo and you should know which interface (en0, en1, etc.) is your Airport adapter. If you don't try the service and macOS will attempt to guess which one it is.

    $ networksetup -setairportpower Wi-Fi off

In the specific case where you don't want auto join, just set the network to not auto join and then toggle the network status using networksetup.

If you just want to disconnect from a WiFi network, disable the adapter:

$ sudo networksetup -setnetworkserviceenabled Wi-Fi off && sleep 15 && sudo networksetup -setnetworkserviceenabled Wi-Fi on

(You can issue the command without sudo but you will get a popup prompting you for a password for each networksetup invocation)

  • Hey Allan, thanks for your response, but for AirDrop to work I need wifi enabled. That's why I specifically wrote 'disconnect'. – Panagiotis Toni Bobolakis Jun 8 '18 at 12:13
  • 2
    @PanagiotisToniBobolakis That level of explanation is best put in the body of the question. If you explain why disabling it and linking to another question here that already has this answer - makes it clear you want to control the SSID association only - not change the power state or – bmike Jun 8 '18 at 12:16
1

You can use this one (found on the internet and tried myself):

Instead of "DEVICE_NAME" use your interface (i.g.: en0)

sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport "DEVICE_NAME" -z

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .