Skip to content

Instantly share code, notes, and snippets.

@JayBrown
Last active May 13, 2020 03:55
Show Gist options
  • Select an option

  • Save JayBrown/851c337222f3d5521901dae1c88d648f to your computer and use it in GitHub Desktop.

Select an option

Save JayBrown/851c337222f3d5521901dae1c88d648f to your computer and use it in GitHub Desktop.

Revisions

  1. JayBrown revised this gist May 13, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wifi-auto-switch.zsh
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ rssi_threshold_24="15" # signal strength difference threshold below which to swi
    # NOTE: threshold values can be identical!
    pw_50ghz="myPassword1" # 5.0 GHz network passphrase
    pw_24ghz="myPassword2" # 2.4 GHz network passphrase
    # NOTE: escape '$' (string/dollar) characters in passphrases with a '\' (backslash)!
    # NOTE: escape '$' (string/dollar) characters with '\' (backslash) in passphrases!
    ##################################

    # macOS airport CLI location
  2. JayBrown revised this gist May 13, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wifi-auto-switch.zsh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    #!/bin/zsh
    # shellcheck shell=bash

    # wifi-auto-switch.zsh v0.2
    # wifi-auto-switch.zsh for macOS
    # v0.2

    emulate -LR bash
    export LANG=en_US.UTF-8
  3. JayBrown revised this gist May 13, 2020. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions wifi-auto-switch.zsh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    #!/bin/zsh
    # shellcheck shell=bash

    # wifi-auto-switch.zsh v0.1
    # wifi-auto-switch.zsh v0.2

    emulate -LR bash
    export LANG=en_US.UTF-8

    # switch WiFi network function
    @@ -37,8 +39,8 @@ suffix_50ghz=".5" # suffix for 5.0 GHz SSID
    suffix_24ghz=".2.4" # suffix for 2.4 GHz SSID
    bssid_5p0ghz="00:00:00:00:00:00" # 5.0 GHz BSSID
    bssid_2p4ghz="00:00:00:00:00:00" # 2.4 GHz BSSID
    rssi_threshold_50="20" # threshold above which to switch to 2.4 GHz
    rssi_threshold_24="10" # threshold below which to switch back to 5.0 GHz
    rssi_threshold_50="20" # signal strength difference threshold above which to switch to 2.4 GHz
    rssi_threshold_24="15" # signal strength difference threshold below which to switch back to 5.0 GHz
    # NOTE: threshold values can be identical!
    pw_50ghz="myPassword1" # 5.0 GHz network passphrase
    pw_24ghz="myPassword2" # 2.4 GHz network passphrase
    @@ -87,7 +89,7 @@ rssi24=$(echo "$scan_ssid" | grep "$bssid_2p4ghz" | awk -F"$bssid_2p4ghz" '{prin
    echo -e "Current 5 GHz RSSI: $rssi50\nCurrent 2.4 GHz RSSI: $rssi24"

    # RSSI difference between 5.0 & 2.4 GHz # always subtract 5.0 from 2.4 because 2.4 GHz is usually stronger
    rssi_diff=$(expr $rssi24 - $rssi50 | tr -d -c 0-9)
    rssi_diff=$(echo "$rssi24 - $rssi50" | bc | tr -d -c 0-9)
    echo "RSSI difference: $rssi_diff"

    # check for network switching
    @@ -111,6 +113,4 @@ elif $net24 ; then
    echo "Value is below threshold: switching WiFi network..."
    _switchwifi 50
    fi
    fi


    fi
  4. JayBrown revised this gist May 12, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wifi-auto-switch.zsh
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ pw_24ghz="myPassword2" # 2.4 GHz network passphrase
    airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport"

    # current WiFi status
    bssid_current=$(airport -I | awk '/BSSID:/{print $2}')
    bssid_current=$("$airport" -I | awk '/BSSID:/{print $2}')
    if [[ $bssid_current == "$bssid_5p0ghz" ]] ; then
    echo "Current BSSID: $bssid_5p0ghz [5 GHz]"
    net50=true
  5. JayBrown created this gist May 12, 2020.
    116 changes: 116 additions & 0 deletions wifi-auto-switch.zsh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    #!/bin/zsh

    # wifi-auto-switch.zsh v0.1

    export LANG=en_US.UTF-8

    # switch WiFi network function
    _switchwifi () {
    # get WiFi device identifier (often 'en0')
    wifidevice=$(networksetup -listnetworkserviceorder | grep "^(Hardware Port: Wi-Fi," | rev | awk '{print $1}' | rev | sed 's/)$//')
    if ! [[ $wifidevice ]] ; then
    echo "Error: WiFi device not found!" >&2
    else # switch network
    error=false
    if [[ $1 == 24 ]] ; then # switch to 2.4 GHz network
    echo "Switching to 2.4 GHz network..."
    if ! networksetup -setairportnetwork "$wifidevice" "$ssid_base$suffix_24ghz" "$pw_24ghz" ; then
    error=true
    fi
    elif [[ $1 == 50 ]] ; then # switch to 5.0 GHz network
    echo "Switching to 5.0 GHz network..."
    if ! networksetup -setairportnetwork "$wifidevice" "$ssid_base$suffix_50ghz" "$pw_50ghz" ; then
    error=true
    fi
    fi
    if $error ; then
    echo "Error switching WiFi network!" >&2
    fi
    fi
    }

    ##################################
    ######### USER VARIABLES #########
    ##################################
    ssid_base="MyNetwork" # basename of the WiFi's SSID (without suffixes)
    suffix_50ghz=".5" # suffix for 5.0 GHz SSID
    suffix_24ghz=".2.4" # suffix for 2.4 GHz SSID
    bssid_5p0ghz="00:00:00:00:00:00" # 5.0 GHz BSSID
    bssid_2p4ghz="00:00:00:00:00:00" # 2.4 GHz BSSID
    rssi_threshold_50="20" # threshold above which to switch to 2.4 GHz
    rssi_threshold_24="10" # threshold below which to switch back to 5.0 GHz
    # NOTE: threshold values can be identical!
    pw_50ghz="myPassword1" # 5.0 GHz network passphrase
    pw_24ghz="myPassword2" # 2.4 GHz network passphrase
    # NOTE: escape '$' (string/dollar) characters in passphrases with a '\' (backslash)!
    ##################################

    # macOS airport CLI location
    airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport"

    # current WiFi status
    bssid_current=$(airport -I | awk '/BSSID:/{print $2}')
    if [[ $bssid_current == "$bssid_5p0ghz" ]] ; then
    echo "Current BSSID: $bssid_5p0ghz [5 GHz]"
    net50=true
    net24=false
    elif [[ $bssid_current == "$bssid_2p4ghz" ]] ; then
    echo "Current BSSID: $bssid_2p4ghz [2.4 GHz]"
    net50=false
    net24=true
    else
    echo "Not connected to home WiFi! Exiting..."
    exit
    fi

    # WiFi environment scan
    while true
    do
    scan_all=$("$airport" -s 2>/dev/null)
    scan_ssid=$(echo "$scan_all" | grep "$ssid_base")
    if ! [[ $scan_ssid ]] ; then
    echo "Home WiFi network '$ssid_base' not detected! Exiting..."
    exit
    else
    if [[ $(echo "$scan_ssid" | wc -l) == 1 ]] ; then
    echo "Only one home WiFi network detected! Exiting..."
    exit
    else
    break
    fi
    fi
    done

    # RSSI values (signal strength)
    rssi50=$(echo "$scan_ssid" | grep "$bssid_5p0ghz" | awk -F"$bssid_5p0ghz" '{print substr($0, index($0,$2))}' | awk '{print $1}' | tr -d -c 0-9)
    rssi24=$(echo "$scan_ssid" | grep "$bssid_2p4ghz" | awk -F"$bssid_2p4ghz" '{print substr($0, index($0,$2))}' | awk '{print $1}' | tr -d -c 0-9)
    echo -e "Current 5 GHz RSSI: $rssi50\nCurrent 2.4 GHz RSSI: $rssi24"

    # RSSI difference between 5.0 & 2.4 GHz # always subtract 5.0 from 2.4 because 2.4 GHz is usually stronger
    rssi_diff=$(expr $rssi24 - $rssi50 | tr -d -c 0-9)
    echo "RSSI difference: $rssi_diff"

    # check for network switching
    if $net50 ; then
    rssi_threshold="$rssi_threshold_50"
    echo "Threshold: $rssi_threshold"
    if [[ $rssi_diff -lt "$rssi_threshold" ]] ; then
    echo "Value is below threshold: exiting..."
    exit
    else # on 5.0 GHz the value needs to be *above* threshold, i.e. decreased strength of 5.0 GHz
    echo "Value is above threshold: switching WiFi network..."
    _switchwifi 24
    fi
    elif $net24 ; then
    rssi_threshold="$rssi_threshold_24"
    echo "Threshold: $rssi_threshold"
    if [[ $rssi_diff -gt "$rssi_threshold" ]] ; then
    echo "Value is above threshold: exiting..."
    exit
    else # on 2.4 GHz the value needs to be *below* threshold, i.e. increased strength of 5.0 GHz
    echo "Value is below threshold: switching WiFi network..."
    _switchwifi 50
    fi
    fi