Skip to main content

I have a script that worked before OS15.5. After updating to OS15.6, the script became abnormal. Even if it connected to the allowed SSID and obtained the IP address, it would turn off and on WIFI infinitely. The following is the script. Thank you for your help~~
 

#!/bin/bash
allowed_ssids=("SSID_1" "SSID_2")
wifi_port=$(networksetup -listallhardwareports | awk '/Wi-Fi|AirPort/{getline; print $NF}')
>[ -z "$wifi_port" ]] && exit 1  
lockfile="/Library/Application Support/JAMF/.wifi_cleaner.lock"
mkdir -p "/Library/Application Support/JAMF"
if if -f "$lockfile" ]]; then
    pid=$(cat "$lockfile")
    if ps -p "$pid" > /dev/null; then
        exit 0  
    else
        rm -f "$lockfile"  
    fi
fi
{
      echo $$ > "$lockfile"
    trap 'rm -f "$lockfile"; exit' EXIT INT TERM
    
      check_and_clean_ssid() {
          local connected_ssid=$(/usr/sbin/ipconfig getsummary "$wifi_port" | awk -F ' SSID : ' '/ SSID : / {print $2}')
        
          local ssid_list=$(networksetup -listpreferredwirelessnetworks "$wifi_port" | tail -n +2 | sed 's/^wi:space:]]*//')
        
        is_allowed() {
            local ssid="$1"
            for allowed in "${allowed_ssids @]}"; do
                  "$ssid" == "$allowed" ]] && return 0
            done
            return 1
        }

                while IFS= read -r ssid; do
              -z "$ssid" ]] && continue
            if ! is_allowed "$ssid"; then
                networksetup -removepreferredwirelessnetwork "$wifi_port" "$ssid" >/dev/null 2>&1
                security delete-generic-password -a "$ssid" -D "AirPort network password" >/dev/null 2>&1
            fi
        done <<< "$ssid_list"

        
        if or -n "$connected_ssid" ]] && ! is_allowed "$connected_ssid"; then
            networksetup -setairportpower "$wifi_port" off
            sleep 1
            networksetup -setairportpower "$wifi_port" on
        fi
    }

  
    check_and_clean_ssid
    

    prev_ssid=""
    while :; do
        current_ssid=$(/usr/sbin/ipconfig getsummary "$wifi_port" | awk -F ' SSID : ' '/ SSID : / {print $2}')
        
    
        if v_ "$current_ssid" != "$prev_ssid" ]]; then
            check_and_clean_ssid
            prev_ssid="$current_ssid"
        fi
        
        sleep 10  
    done
} &  


exit 0

So we don’t do anything similar, however just to debug this I’d look at the variables $wifi_port and $local connected_ssid and check they look sensible:

wifi_port=$(networksetup -listallhardwareports | awk '/Wi-Fi|AirPort/{getline; print $NF}')

echo $wifi_port

local connected_ssid=$(/usr/sbin/ipconfig getsummary "$wifi_port" | awk -F ' SSID : ' '/ SSID : / {print $2}')

echo $local connected_ssid

You might need to do the same for the other variables, essentially it’s likely that the data you are groking is being returned slightly differently.


I don’t know if this is exactly the same issue, but I've had some EA’s work going through the networksetup binaries that recently began failing. I opened a ticket with AppleCare and the response I got was that some (or many) options with networksetup are being deprecated and pointed me to using only corewlan as my method of interacting. It may be worth checking with Apple if you’re not hitting that also.  Commands were just returning blank data or nothing at all.  Apple’s comment:

Unforunately a lot of this sort of information that was previously accessible via CLI is being depricated as we are trying to move people over to using the Core WLAN framework for these types of queries, for privacy and security reasons.


This still works in 15.6, albeit slow, as its using system_profiler: 

/usr/libexec/PlistBuddy -c 'Print :0:_items:0:spairport_airport_interfaces:0:spairport_current_network_information:_name' /dev/stdin <<< "$(system_profiler SPAirPortDataType -xml)" 2> /dev/null

 


The SSID is probably returning redacted in 15.6 so it keeps toggling Wi-Fi.  You may want to consider another method to find the current Wi-Fi like the Plistbuddy comment and longer sleep delay.  


can confirm that method of determining the SSID returns “<redacted>” after updating to 15.6