Skip to content

Instantly share code, notes, and snippets.

@leggiero
Last active August 31, 2023 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leggiero/9519ccecae3c543d921a04aa42e77251 to your computer and use it in GitHub Desktop.
Save leggiero/9519ccecae3c543d921a04aa42e77251 to your computer and use it in GitHub Desktop.
Change the Network Manager connection stable-id of the current connected Wi-Fi to control the privacy.
#!/usr/bin/bash
# The MIT License (MIT)
# Copyright 2023 Eduardo Leggiero
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -e
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
fixed_stable_id="\${CONNECTION}/\${DEVICE}"
action=$1
custom_stable_id=$2
if [[ ( "$#" == 1 && ( "$1" == "help" || "$1" == "--help" || "$1" == "-h" ) ) || \
! ( "$#" == 0 || \
( "$#" == 1 && ( "$action" == "fixed" || "$action" == "default" || "$action" == "show" ) ) || \
( "$#" == 2 && ( "$action" == "custom" && "$custom_stable_id" != "" ) ) \
) ]]; then
>&2 echo "Usage: $me [fixed | default | show]"
>&2 echo " $me custom [stable-id]"
>&2 echo " $me [help | --help | -h]"
exit 1
fi
[[ -z "$action" ]] && action="show"
current_wifi="$(nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d: -f2)"
if [[ -z "$current_wifi" ]]; then
>&2 echo -e "\033[0;31mNot connected to a Wi-Fi connection.\033[0m"
exit 1
fi
echo -e "Current Wi-Fi connection: \033[0;36m$current_wifi\033[0m"
connection_uuid="$(nmcli c show | awk -F' +' -v uuid="$current_wifi" '$3 == "wifi" && $1 == uuid {print $2}')"
echo -e "Network Manager UUID: \033[0;36m$connection_uuid\033[0m"
current_stable_id="$(nmcli c show "$connection_uuid" | grep connection.stable-id | awk '{print $2}')"
[[ "$current_stable_id" == "--" ]] && current_stable_id=""
echo -ne "Current stable-id value: "
if [[ "$current_stable_id" == "" ]]; then
echo -ne "\033[0;35m\033[3mdefault\033[0m"
else
echo -ne "\033[0;36m$current_stable_id\033[0m"
fi
echo -ne " \033[0;34m\033[3m("
if [[ "$current_stable_id" == "$fixed_stable_id" ]]; then
echo -n "fixed"
elif [[ "$current_stable_id" == "" ]]; then
echo -n "default"
else
echo -n "custom"
fi
echo -e ")\033[0m"
if [[ "$action" == "show" ]]; then
exit 0
elif [[ "$action" == "fixed" ]]; then
new_stable_id="$fixed_stable_id"
elif [[ "$action" == "default" ]]; then
new_stable_id=""
elif [[ "$action" == "custom" ]]; then
new_stable_id="$custom_stable_id"
fi
if [[ "$current_stable_id" == "$new_stable_id" ]]; then
echo -ne "\033[0;33mNot changed as stable-id is already "
if [[ "$action" == "custom" ]]; then
echo -ne "the custom value \"$custom_stable_id\""
else
echo -ne "$action"
fi
echo -e ".\033[m"
exit
fi
echo -ne "New stable-id value: "
if [[ "$new_stable_id" == "" ]]; then
echo -ne "\033[0;35m\033[3mdefault\033[0m"
else
echo -ne "\033[0;36m$new_stable_id\033[0m"
fi
echo -ne " \033[0;34m\033[3m("
if [[ "$new_stable_id" == "$fixed_stable_id" ]]; then
echo -n "fixed"
elif [[ "$new_stable_id" == "" ]]; then
echo -n "default"
else
echo -n "custom"
fi
echo -e ")\033[0m"
nmcli c modify "$connection_uuid" connection.stable-id "$new_stable_id"
echo -e "\033[0;32mChanged!\033[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment