script to remove them from wifi and add them back

diegogut90
New Contributor III

so i got to remove the SSID from the preferred list but it still is connected to that SSID, is there anyway i could shut down wifi and turn it back on real quick. that way they could connect to the actual wifi i want them too. ?

1 ACCEPTED SOLUTION

boberito
Valued Contributor

I actually have a script for doing just this. Set this up as a policy to run once on the computer to install it

https://github.com/boberito/jamfscripts/blob/master/RemoveGuest%20-%20LaunchDeamon.sh

You can replace GUEST with your wifi network you want to remove. YOURCOMPANY with whatever you want. And you can change the location to somewhere other than /usr/local/

This will install a launchdaemon that'll watch a the file that gets changed when you add a wifi network. Check if the specific network is added. Then it'll remove it, or if it's currently connected to that network it'll turn wifi off for a moment then remove it and back on.

#!/bin/sh

#Sets up a script and creates a launchdeamon that watches com.apple.airport.preferences.plist
#This changes whenever you join a network. If the Guest network is joined, then remove it and bop them off of it


##################
##CREATE SCRIPT###
##################
cat << EOF > /usr/local/removeGuest.sh
#!/bin/bash

#set interface name and network you're hunting for
interfaceName="Wi-Fi"
networkName="GUEST"

Adapter=$(networksetup -listallhardwareports | grep -A 1 "$interfaceName" | grep Device | awk '{print $2}')

if networksetup -listpreferredwirelessnetworks $Adapter | grep "$networkName"; then
    echo "Guest Found"
    ConnectedtoGuest=$(networksetup -getairportnetwork $Adapter | awk -F ":" '{ print $2 }' | cut -c 2-)
    if [ "$ConnectedtoGuest" == "$networkName" ]; then
            #Gotta disconnect first to remove it
            networksetup -setairportpower $Adapter off
            networksetup -removepreferredwirelessnetwork $Adapter "$networkName"
            networksetup -setairportpower $Adapter on

    else
            networksetup -removepreferredwirelessnetwork $Adapter "$networkName"

    fi
fi

EOF

########################
##CREATE LAUNCHDAEMON###
########################

cat << EOF > /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.YOURCOMPANY.removeguest2</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>/usr/local/removeGuest.sh</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
    </array>
</dict>
</plist>

EOF

chown root:wheel /usr/local/removeGuest.sh
chmod 755 /usr/local/removeGuest.sh

chown root:wheel /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist
chmod 644 /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist

launchctl load -w /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist

View solution in original post

11 REPLIES 11

ryan_ball
Valued Contributor
#!/bin/bash

wifiAdapter=$(/usr/sbin/networksetup -listallhardwareports | egrep -A 2 "Hardware Port: (Air|Wi-)" | grep "Device:" | awk '{print $2}')
wifiPower=$(/usr/sbin/networksetup -getairportpower "$wifiAdapter" | awk '{print $NF}')
if [[ "$wifiPower" = "On" ]]; then
    echo "Toggling Wi-Fi adapter..."
    /usr/sbin/networksetup -setairportpower "$wifiAdapter" off
    sleep 5
    /usr/sbin/networksetup -setairportpower "$wifiAdapter" on
fi

exit

boberito
Valued Contributor

I actually have a script for doing just this. Set this up as a policy to run once on the computer to install it

https://github.com/boberito/jamfscripts/blob/master/RemoveGuest%20-%20LaunchDeamon.sh

You can replace GUEST with your wifi network you want to remove. YOURCOMPANY with whatever you want. And you can change the location to somewhere other than /usr/local/

This will install a launchdaemon that'll watch a the file that gets changed when you add a wifi network. Check if the specific network is added. Then it'll remove it, or if it's currently connected to that network it'll turn wifi off for a moment then remove it and back on.

#!/bin/sh

#Sets up a script and creates a launchdeamon that watches com.apple.airport.preferences.plist
#This changes whenever you join a network. If the Guest network is joined, then remove it and bop them off of it


##################
##CREATE SCRIPT###
##################
cat << EOF > /usr/local/removeGuest.sh
#!/bin/bash

#set interface name and network you're hunting for
interfaceName="Wi-Fi"
networkName="GUEST"

Adapter=$(networksetup -listallhardwareports | grep -A 1 "$interfaceName" | grep Device | awk '{print $2}')

if networksetup -listpreferredwirelessnetworks $Adapter | grep "$networkName"; then
    echo "Guest Found"
    ConnectedtoGuest=$(networksetup -getairportnetwork $Adapter | awk -F ":" '{ print $2 }' | cut -c 2-)
    if [ "$ConnectedtoGuest" == "$networkName" ]; then
            #Gotta disconnect first to remove it
            networksetup -setairportpower $Adapter off
            networksetup -removepreferredwirelessnetwork $Adapter "$networkName"
            networksetup -setairportpower $Adapter on

    else
            networksetup -removepreferredwirelessnetwork $Adapter "$networkName"

    fi
fi

EOF

########################
##CREATE LAUNCHDAEMON###
########################

cat << EOF > /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.YOURCOMPANY.removeguest2</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>/usr/local/removeGuest.sh</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
    </array>
</dict>
</plist>

EOF

chown root:wheel /usr/local/removeGuest.sh
chmod 755 /usr/local/removeGuest.sh

chown root:wheel /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist
chmod 644 /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist

launchctl load -w /Library/LaunchDaemons/com.YOURCOMPANY.removeguest.plist

diegogut90
New Contributor III

@boberito it worked but also gave me this Script result as 6e79178bee624bbcb227a4b38340af33
what does that mean?

boberito
Valued Contributor

HA! I left my username in there...my bad!

Change the line chown YOURADMINNAME:wheel /usr/local/removeGuest.sh

Or change chown root:wheel /usr/local/removeGuest.sh

diegogut90
New Contributor III

@boberito is there a way to add to that script above to basically change the priorty ssid to 0. so that the main wifi is the always the main one.

sdagley
Esteemed Contributor II

@diegogut90 If you want to enforce an order to preferred WiFi list take a look at Re-order WIFI Preferred Networks...

boberito
Valued Contributor

In my script no. To do that via bash as far as I know you have to remove and re-add the network at a higher priority.

That may work for some, but probably not most and it wouldn't work the environment where I wrote this originally for which was a WPA2 Enterprise using radius and all.

sdagley
Esteemed Contributor II

@boberito The python script I reference above can reorder WiFi configurations with WPA2 Enterprise security.

boberito
Valued Contributor

@sdagley I know I saw...that's why I said in my script and with bash.

I'm interested in that python script. I may dig deeper into it and try to understand it because it may be doable to combine both. I just gotta get my pythoning stronger.

sdagley
Esteemed Contributor II

@boberito I'm a firm believer in never write a script from scratch if you can steal borrow most of what you need from something already written by someone else. The original script came from Pudquick (who doesn't appear to be a Jamf Nation member). I tweaked it to use an array of SSIDs rather than being hard coded for only 3. Now it's your turn :-)

boberito
Valued Contributor

@sdagley isn't that object oriented coding? haha...resuable coding.