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

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

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

Local Network via File sharing permission problems

Updated December 14, 2017 15:12 PM

Airdrop is not working with my Mac

Updated June 21, 2016 08:05 AM