Skip to content

Instantly share code, notes, and snippets.

@luqasn
Created November 3, 2022 12:51
Show Gist options
  • Save luqasn/e5aff3542a0d8a4794e4bfe9e90cfbf5 to your computer and use it in GitHub Desktop.
Save luqasn/e5aff3542a0d8a4794e4bfe9e90cfbf5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# DNS Switcher
# The list of DNS options should be defined on this file
#
# rewrite of https://github.com/matryer/bitbar-plugins/blob/master/Network/dnsswitcher.1d.sh
# by M Saiqul Haq (@saiqulhaq)
#
# <xbar.title>DNS Switcher</xbar.title>
# <xbar.version>v2.1.7-beta</xbar.version>
# <xbar.author>Lucas Romero</xbar.author>
# <xbar.author.github>luqasn</xbar.author.github>
# <xbar.desc>Switch DNS to your defined DNS options.</xbar.desc>
# Configuration
# add or remove list of DNS options below, don't forget to make it enabled. see below
# shellcheck disable=2034
cloudflare="1.1.1.1 1.0.0.1"
# shellcheck disable=2034
google="8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844"
# shellcheck disable=2034
level3="209.244.0.3 209.244.0.4 4.2.2.1 4.2.2.2 4.2.2.3 4.2.2.4"
# shellcheck disable=2034
opendns="208.67.222.222 208.67.220.220"
# shellcheck disable=2034
norton="199.85.126.10 199.85.127.10 199.85.126.20 199.85.127.20 199.85.126.30 199.85.127.30"
localhost="127.0.0.1"
# shellcheck disable=2034
default="empty"
enabled_dns_address=(default localhost)
########################
get_interface() {
while read -r line; do
sname="$(echo "$line" | sed -E 's|(.*) \(Hardware Port: (.+), Device: (.+)\)|\1|')"
sdev="$(echo "$line" | sed -E 's|(.*) \(Hardware Port: (.+), Device: (.+)\)|\3|')"
if [ -n "$sdev" ]; then
ifout="$(ifconfig "$sdev" 2>/dev/null)"
echo "$ifout" | grep 'status: active' > /dev/null 2>&1
rc="$?"
if [ "$rc" -eq 0 ]; then
echo "$sname"
break
fi
fi
done <<< "$(networksetup -listnetworkserviceorder | awk 'BEGIN { RS = "\\([0-9]\+\\) " ; FS = "\n" } { print $1, $2 }')"
if [ -z "$currentservice" ]; then
>&2 echo "Could not find current service"
exit 1
fi
}
flushcache(){
killall -HUP mDNSResponder &>/dev/null
sudo killall mDNSResponderHelper &>/dev/null
sudo dscacheutil -flushcache &>/dev/null
}
selected_dns="Unknown"
network_service="$(get_interface)"
current_dns_output=$(networksetup -getdnsservers "$network_service")
if [[ $current_dns_output == There* ]] # For e.g. "There aren't any DNS Servers set on Wi-Fi."
then
selected_dns="default"
else
IFS=', ' read -r -a current_dns_address <<< "$current_dns_output"
for dns_name in "${enabled_dns_address[@]}"
do
for current_dns in "${current_dns_address[@]}"
do
dns_option="${!dns_name}"
if [[ $dns_option == *"$current_dns"* ]]
then
selected_dns="$dns_name"
fi
done
done
fi
get_interfaces
return
### Bitbar Menu
if [[ $selected_dns == "Unknown" ]]
then
echo "Unrecognized DNS"
else
echo "$selected_dns"
fi
echo "---"
for dns_name in "${enabled_dns_address[@]}"
do
echo "$dns_name | shell=$0 | param1='${dns_name}' | terminal=false | refresh=true"
done
case "$0" in
*\ * )
echo "Your script path | color=#ff0000"
echo "($0) | color=#ff0000"
echo "has a space in it, which BitBar does not support. | color=#ff0000"
;;
esac
if [ ! -z "$1" ]; then
networksetup -setdnsservers "$network_service" ${!1}
/usr/bin/osascript -e "do shell script \"flushcache\" with prompt \"DNS caches should be flushed after switching servers.\" with administrator privileges"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment