Last active
August 27, 2023 09:25
-
-
Save leggiero/251c565bad119870136a3949a34b48fe to your computer and use it in GitHub Desktop.
NetworkManager dispatcher script to change DNS servers only after connecting to public Wi-Fi networks, to allow Captive portals that requires its own DNS to work properly. To be saved in /etc/NetworkManager/dispatcher.d. Tested on Fedora Workstation 38.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
debug=0 | |
info=0 | |
notification=0 | |
ignore_ssid=("My-Home-Wi-Fi" "My-Work-Wi-Fi") | |
notify_message="Using Google Public DNS servers" | |
notification_user_id="1000" | |
main() { | |
journal_identifier="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" | |
[[ "$journal_identifier" =~ ^[0-9]+-(.+)$ ]] && journal_identifier="${BASH_REMATCH[1]}" | |
if [[ "$debug" == "1" ]]; then | |
message="NetworkManager event: Action: $NM_DISPATCHER_ACTION" | |
[[ ! -z "$CONNECTIVITY_STATE" ]] && message="$message, State: $CONNECTIVITY_STATE" | |
[[ ! -z "$CONNECTION_ID" ]] && message="$message, Id: $CONNECTION_ID" | |
debug "$message" | |
fi | |
if [[ "$NM_DISPATCHER_ACTION" == "up" || \ | |
( "$NM_DISPATCHER_ACTION" == "connectivity-change" && \ | |
"$CONNECTIVITY_STATE" == "FULL" ) ]]; then | |
ssid="$(nmcli -t -f active,ssid dev wifi | grep -E '^yes' | cut -d: -f2)" | |
if [[ "$NM_DISPATCHER_ACTION" == "up" ]]; then | |
if [[ "$debug" == "1" && -z "$ssid" ]]; then | |
debug "Ignored as it is not a Wi-Fi connection: \"$CONNECTION_ID\"" | |
fi | |
elif [[ ! -z "$ssid" ]]; then | |
[[ "$debug" == "1" ]] && debug "Wi-Fi SSID: \"$ssid\"" | |
if [[ ! " ${ignore_ssid[*]} " =~ " $ssid " ]]; then | |
[[ "$info" == "1" ]] && info "Changing DNS servers for \"$ssid\" Wi-Fi connection" | |
# Note: 8.8.8.8 is 134744072 and 8.8.4.4 is 134743044 in decimal | |
dbus-send --system --dest=org.freedesktop.NetworkManager.dnsmasq \ | |
/uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \ | |
uint32:134744072 uint32:134743044 | |
sed -i '/^search /d' /etc/resolv.conf | |
[[ "$notification" == "1" ]] && sudo -u "#$notification_user_id" \ | |
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/notification_user_id/bus" \ | |
notify-send --transient --app-name "$journal_identifier" "$notify_message" || true | |
elif [[ "$debug" == "1" ]]; then | |
debug "Wi-Fi SSID ignored: \"$ssid\"" | |
fi | |
fi | |
fi | |
} | |
log() { | |
echo "$1" | systemd-cat -t "$journal_identifier" -p "$2" | |
} | |
info() { | |
log "$1" info | |
} | |
debug() { | |
log "$1" debug | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment