summaryrefslogtreecommitdiff
path: root/scripts/pihole-dns-toggle
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/pihole-dns-toggle')
-rwxr-xr-xscripts/pihole-dns-toggle97
1 files changed, 65 insertions, 32 deletions
diff --git a/scripts/pihole-dns-toggle b/scripts/pihole-dns-toggle
index 1e3cc4d..74770b1 100755
--- a/scripts/pihole-dns-toggle
+++ b/scripts/pihole-dns-toggle
@@ -4,7 +4,7 @@
set -e
# Pi-hole DNS servers on the Raspberry Pi pair, with the router as last-resort fallback.
-PIHOLE_DNS="192.168.1.127 192.168.1.128 192.168.1.1"
+PIHOLE_DNS="192.168.1.127 192.168.1.128 192.168.1.101 192.168.1.1"
# Colors for output
GREEN='\033[0;32m'
@@ -12,44 +12,77 @@ YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
-# Get active connection name (excluding loopback)
-get_active_connection() {
- nmcli -t -f NAME,DEVICE connection show --active | grep -v ':lo$' | head -1 | cut -d: -f1
+# Get active Ethernet/Wi-Fi connection names.
+get_active_connections() {
+ nmcli -t -f NAME,DEVICE,TYPE connection show --active | \
+ awk -F: '$2 != "lo" && ($3 == "802-3-ethernet" || $3 == "802-11-wireless") { print $1 }'
}
# Check if Pi-hole DNS is currently enabled
is_pihole_enabled() {
local connection="$1"
- local ignore_auto_dns=$(nmcli -t -f ipv4.ignore-auto-dns connection show "$connection" | cut -d: -f2)
- [[ "$ignore_auto_dns" == "yes" ]]
+ local ignore_auto_dns
+ local configured_dns
+
+ ignore_auto_dns=$(nmcli -g ipv4.ignore-auto-dns connection show "$connection")
+ configured_dns=$(nmcli -g ipv4.dns connection show "$connection" | tr ',' ' ')
+
+ [[ "$ignore_auto_dns" == "yes" && "$configured_dns" == "$PIHOLE_DNS" ]]
}
# Enable Pi-hole DNS
enable_pihole() {
- local connection="$1"
- echo -e "${YELLOW}Enabling Pi-hole DNS for connection: $connection${NC}"
- nmcli con mod "$connection" ipv4.dns "$PIHOLE_DNS"
- nmcli con mod "$connection" ipv4.ignore-auto-dns yes
- nmcli con up "$connection" > /dev/null 2>&1
+ local connection
+
+ for connection in "$@"; do
+ echo -e "${YELLOW}Enabling Pi-hole DNS for connection: $connection${NC}"
+ nmcli con mod "$connection" ipv4.dns "$PIHOLE_DNS"
+ nmcli con mod "$connection" ipv4.ignore-auto-dns yes
+ nmcli con up "$connection" > /dev/null 2>&1
+ done
+
echo -e "${GREEN}✓ Pi-hole DNS enabled${NC}"
echo "DNS servers: $PIHOLE_DNS"
}
# Disable Pi-hole DNS (use DHCP-provided DNS)
disable_pihole() {
- local connection="$1"
- echo -e "${YELLOW}Disabling Pi-hole DNS for connection: $connection${NC}"
- nmcli con mod "$connection" ipv4.dns ""
- nmcli con mod "$connection" ipv4.ignore-auto-dns no
- nmcli con up "$connection" > /dev/null 2>&1
+ local connection
+
+ for connection in "$@"; do
+ echo -e "${YELLOW}Disabling Pi-hole DNS for connection: $connection${NC}"
+ nmcli con mod "$connection" ipv4.dns ""
+ nmcli con mod "$connection" ipv4.ignore-auto-dns no
+ nmcli con up "$connection" > /dev/null 2>&1
+ done
+
echo -e "${GREEN}✓ Pi-hole DNS disabled (using DHCP DNS)${NC}"
}
+all_connections_pihole_enabled() {
+ local connection
+
+ for connection in "$@"; do
+ if ! is_pihole_enabled "$connection"; then
+ return 1
+ fi
+ done
+
+ return 0
+}
+
# Show current DNS status
show_status() {
- local connection="$1"
echo -e "\n${YELLOW}Current DNS configuration:${NC}"
- nmcli dev show | grep "IP4.DNS" | head -4
+ nmcli dev show | awk -F: '
+ function trim(value) {
+ sub(/^[[:space:]]+/, "", value)
+ sub(/[[:space:]]+$/, "", value)
+ return value
+ }
+ /^GENERAL.DEVICE:/ { device=trim($2) }
+ /^IP4.DNS/ { print device ": " trim($2) }
+ '
}
# Check if running on Fedora
@@ -75,41 +108,41 @@ main() {
exit 1
fi
- # Get active connection
- CONNECTION=$(get_active_connection)
+ # Get active connections
+ mapfile -t CONNECTIONS < <(get_active_connections)
- if [[ -z "$CONNECTION" ]]; then
+ if [[ "${#CONNECTIONS[@]}" -eq 0 ]]; then
echo -e "${RED}Error: No active network connection found${NC}"
exit 1
fi
- echo "Active connection: $CONNECTION"
+ echo "Active connections: ${CONNECTIONS[*]}"
# Handle command-line arguments
case "${1:-toggle}" in
on|enable)
- enable_pihole "$CONNECTION"
- show_status "$CONNECTION"
+ enable_pihole "${CONNECTIONS[@]}"
+ show_status
;;
off|disable)
- disable_pihole "$CONNECTION"
- show_status "$CONNECTION"
+ disable_pihole "${CONNECTIONS[@]}"
+ show_status
;;
status)
- if is_pihole_enabled "$CONNECTION"; then
+ if all_connections_pihole_enabled "${CONNECTIONS[@]}"; then
echo -e "${GREEN}Pi-hole DNS is currently ENABLED${NC}"
else
echo -e "${YELLOW}Pi-hole DNS is currently DISABLED${NC}"
fi
- show_status "$CONNECTION"
+ show_status
;;
toggle|*)
- if is_pihole_enabled "$CONNECTION"; then
- disable_pihole "$CONNECTION"
+ if all_connections_pihole_enabled "${CONNECTIONS[@]}"; then
+ disable_pihole "${CONNECTIONS[@]}"
else
- enable_pihole "$CONNECTION"
+ enable_pihole "${CONNECTIONS[@]}"
fi
- show_status "$CONNECTION"
+ show_status
;;
esac
}