Posted on 01-23-2020 07:27 AM
I use the following script originally created by @delbrown here: Manage WiFi on macOS
I modified it to warn the users using JAMFHelper and allow them to stay connected.
I would like to add some logging to it so I can see how many times a user connects to a non-work network and how often they stay connected or disconnect.
Any help is much appreciated.
#!/bin/bash
# Monitor and Manage WiFi Networks
# Del Brown
# 11/21/16
# delonline@icloud.com
# https://www.jamf.com/jamf-nation/discussions/22083/title
# modified 01/24/2020 to warn user rather than disconnect by Corey Ammons.
# Begin Variable Definitions
#Replace SSID with your work WiFi. Leaving it empty will disconnect from any network.
# Set your own network names here
WifiWhitelist="SSID1 SSID2 SSID3"
wifiInterface=$(networksetup -listallhardwareports | grep 'Wi-Fi' -A1 | grep -o en.)
# End of Variable Definitions
# Begin Function Declarations
connect ()
{
for WorkNetwork in $WifiWhitelist
do
sleep 5
networksetup -setairportpower ${wifiInterface} on
done
}
disconnect ()
{
echo "Time to disconnect"
# send disconnect command to en0
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -z
#exit 1
}
onWorkNetwork ()
{
# test to see if the work network has been joined. You can either use the networksetup command or the airport utility for this
MyWifi=`networksetup -getairportnetwork ${wifiInterface} | awk '{print $4}'`
#MyWifi=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport --getinfo | grep " SSID" | awk '{print $2}'`
# Warn the user if connected to a non work wifi while at work.
for AllowedID in $WifiWhitelist
do
if [ "$AllowedID" == "$MyWifi" ]
then # Asset is on the work network
echo "I am connected to the Work Network ""$AllowedID"
exit 1
fi
done
echo "Device is not connected to Work Network so warn user to reconnect to Tingo"
JAMFHELPER="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
# Get the user's selection
# Set your warning message here
RESULT=`"$JAMFHELPER" -windowType hud -lockHUD -title "Wifi Warning" -description "Set your warning message here" -button1 "Not Yet" -button2 "Disconnect" -defaultButton 1 `
if [ $RESULT == 0 ]; then
echo "OK was pressed!"
elif [ $RESULT == 2 ]; then
disconnect
networksetup -setairportpower ${wifiInterface} off
connect
echo "Disconnect was pressed!"
fi
exit
}
atWork ()
{
WifiAvailable=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s | awk '{print $1}'`
# test to see if the Asset is at work by scanning for work networks and see if one is the work
for ScannedNetworks in $WifiAvailable
do
for WorkNetwork in $WifiWhitelist
do
if [ "$WorkNetwork" == "$ScannedNetworks" ]
then
AssetAtWork="Yes"
echo "The Device is at work"
return
fi
done
done
AssetAtWork="No"
echo "asset is not at work so we don't care and we'll exit"
exit
}
# End Function Declarations
#########################
#program starts here
atWork
onWorkNetwork