Created
May 24, 2021 21:02
-
-
Save rawiriblundell/8dda3a9791a12303632a43de0b306921 to your computer and use it in GitHub Desktop.
A function that attempts to list the dns servers that are used by a host
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
dnslist() { | |
case $(uname) in | |
(Darwin) | |
printf -- '%s\n' "Attempting lookup test using 'scutil' command..." >&2 | |
scutil --dns | | |
awk '/nameserver/{ a[$3]++} END { for (b in a) {print b } }' | |
return 0 | |
;; | |
([Ll]inux) | |
# TODO: Update to test against IP addresses rather than 'Global' | |
if command -v resolvectl >/dev/null 2>&1; then | |
printf -- '%s\n' "Attempting lookup test using 'resolvectl' command..." >&2 | |
# shellcheck disable=SC2046 | |
set -- $(resolvectl dns | grep Global) | |
printf -- '%s\n' "${@:2}" | |
return 0 | |
fi | |
if command -v systemd-resolv >/dev/null 2>&1; then | |
printf -- '%s\n' "Attempting lookup test using 'systemd-resolve' command..." >&2 | |
# shellcheck disable=SC2046 | |
set -- $( | |
systemd-resolve --status | | |
awk '/DNS Server:/{flag=1;next}/DNS Domain/{flag=0}flag' | | |
paste -sd ' ' - | | |
tr -s '[:space:]' | |
) | |
printf -- '%s\n' "${@:3}" | |
return 0 | |
fi | |
if command -v nmcli >/dev/null 2>&1; then | |
# printf -- '%s\n' "Attempting lookup test using 'nmcli' command..." >&2 | |
: | |
fi | |
;; | |
esac | |
if command -v host >/dev/null 2>&1; then | |
printf -- '%s\n' "Attempting lookup test using 'host' command..." >&2 | |
host -v something.unknown | awk -F "[ #]" '/Received /{print$5}' | uniq | |
return 0 | |
fi | |
if [ -r /etc/resolv.conf ]; then | |
printf -- '%s\n' "Parsing /etc/resolv.conf..." >&2 | |
awk '/nameserver/{print $2}' /etc/resolv.conf | |
return 0 | |
fi | |
# If we get to this point, we have failed. | |
printf -- '%s\n' "Unable to determine any dns servers" >&2 | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment