This script enhances common terminal commands like curl and ping by displaying the DNS resolver used to resolve the hostname. It checks whether the hostname was resolved using:
/etc/hosts- A specific DNS server
- Automatically detects if
/etc/hostswas used for resolution. - Queries configured DNS servers to determine which was used.
- Works seamlessly as a wrapper for
curlandpingcommands.
- macOS or a Unix-like operating system with the following tools installed:
scutil(default on macOS)digawk
-
Copy the script into your shell configuration file (e.g.,
~/.zshrcor~/.bashrc).resolve_source() { local hostname="$1" # Check if the hostname is in /etc/hosts if grep -q "$hostname" /etc/hosts; then echo "Resolved using /etc/hosts" return fi # Use scutil to determine DNS server and query resolution local dns_servers dns_servers=$(scutil --dns | grep "nameserver\[[0-9]*\]" | awk '{print $3}') # Use dig to query the hostname for server in $dns_servers; do if dig @"$server" +short "$hostname" > /dev/null 2>&1; then echo "Resolved using DNS server: $server" return fi done echo "Resolver source unknown" } resolver_info() { local hostname="$1" resolve_source "$hostname" # Run the actual command shift "$@" } curl_resolver() { local host="$(echo "$1" | sed 's|http[s]*://||' | cut -d/ -f1)" resolver_info "$host" curl "$@" } ping_resolver() { resolver_info "$1" ping "$@" } alias curlr="curl_resolver" alias pingr="ping_resolver"
-
Save the file.
-
Reload your shell configuration:
source ~/.zshrc # or ~/.bashrc depending on your shell
The script provides the following aliases:
curlr: Enhancedcurlcommand with resolver information.pingr: Enhancedpingcommand with resolver information.
Curl Example:
curlr http://google.comOutput:
Resolved using DNS server: 8.8.8.8
[Standard curl output]
curlr http://domaininmyhostfile.comOutput:
Resolved using /etc/hosts
[Standard curl output]
Ping Example:
pingr google.comOutput:
Resolved using DNS server: 8.8.8.8
PING google.com (127.0.0.1): 56 data bytes
[Standard ping output]
pingr http://domaininmyhostfile.comOutput:
Resolved using /etc/hosts
[Standard ping output]
- The script uses
scutil --dnsto retrieve DNS server information on macOS. On other systems, you may need to replace this with an equivalent command (e.g.,systemd-resolve --statusorresolvectl). - Ensure
digis installed and available in yourPATH. On macOS, you can install it via Homebrew:brew install bind
Feel free to modify the aliases or add new functions for other commands that involve hostname resolution. For example, you could extend it to wget or ssh.
- DNS resolution issues: Ensure your system's DNS settings are configured correctly.
dignot found: Installdigusing your system's package manager (e.g., Homebrew, apt, or yum).
This script is open source and provided "as is" without warranty. Feel free to modify and distribute it under the MIT License.