# Define log file
log_file="/Library/Logs/bannedwifi.log"
# Function to log messages with timestamps
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$log_file"
}
log "Starting Wi-Fi check script..."
# List of banned SSIDs
banned_ssids=("SSID-1" "SSID-2" "SSID-3" "SSID-4")
# Get the currently logged-in user
log "Detecting current user..."
loggedInUser=$("/usr/bin/stat" -f%Su "/dev/console")
log "Current user: $loggedInUser"
# Get the Wi-Fi interface (usually en0 or en1)
log "Fetching Wi-Fi interface..."
wifiinterface=$(networksetup -listallhardwareports | awk '/Wi-Fi|AirPort/{getline; print $2}')
if [[ -z "$wifiinterface" ]]; then
log "ERROR: Could not detect Wi-Fi interface."
exit 1
fi
log "Found Wi-Fi interface: '$wifiinterface'"
# Get the current SSID
log "Checking current SSID..."
currentssid=$(ipconfig getsummary "$wifiinterface" | awk -F ' SSID : ' '/ SSID : / {print $2}')
if [[ -z "$currentssid" ]]; then
log "No SSID detected. Not connected to a Wi-Fi network."
exit 0
fi
log "Current SSID: '$currentssid'"
# Check if current SSID is in the banned list
if printf '%s\n' "${banned_ssids[@]}" | grep -qxF "$currentssid"; then
log "Connected to banned network '$currentssid'. Proceeding to disconnect and remove..."
# Notify user with a popup
osascript -e "display dialog \"You are not permitted to connect this device to '$currentssid'.\" buttons {\"OK\"} default button 1 with icon caution"
log "Removing '$currentssid' from preferred networks..."
networksetup -removepreferredwirelessnetwork "$wifiinterface" "$currentssid"
log "Turning Wi-Fi off..."
networksetup -setairportpower "$wifiinterface" off
sleep 2
log "Turning Wi-Fi back on..."
networksetup -setairportpower "$wifiinterface" on
log "'$currentssid' removed and Wi-Fi restarted."
else
log "Not connected to a banned network. No action needed."
fi