Script Wi-Fi SSID switch if on SSID "X"

by Mat   Last Updated August 13, 2019 16:12 PM

I'm stuck on a script to switch a Mac to a different SSID if its already connected to a certain SSID.

What I need the script to do is:

Check what SSID the Mac Wi-Fi is using

If it is on SSID called "CompanyA" switch to SSID called "CompanyB" If it is not on SSID called "CompanyA" do nothing.

I can get SSID currently connected to with

networksetup -getairportnetwork en0 | cut -d ":" -f2

and I can switch with

networksetup -setairportnetwork en0 "CompanyB"

But I don't know how to do the if on CompanyA then switch to CompanyB

Tags : mac network

Answers 2

Test ([) if the output of networksetup is the same as the string CompanyA if so then change the SSID to CompanyB.

curssid=$(networksetup -getairportnetwork en0 | cut -d ' ' -f4-)

if [ "$curssid" = CompanyA ]
networksetup -setairportnetwork en0 "CompanyB"
August 13, 2019 15:08 PM

Here is a way to do it:

[[ $(networksetup -getairportnetwork en0 | awk -F': ' '{print $2}') == "CompanyA" ]] && networksetup -setairportnetwork en0 "CompanyB"
August 13, 2019 15:11 PM

Related Questions

Updated September 26, 2017 16:12 PM

Updated March 30, 2016 08:05 AM

Updated June 04, 2017 07:12 AM

Updated December 14, 2017 15:12 PM

Updated June 21, 2016 08:05 AM