Back to Resources
Level
Script
General
Misconfigured DNS servers can compromise network performance, introduce security vulnerabilities, and lead to connectivity issues. Maintaining consistent DNS configurations across multiple macOS devices is critical, especially when you need to ensure compliance or minimize potential vulnerabilities from unauthorized DNS settings.
This script retrieves the currently configured DNS servers on a macOS machine using the scutil
command, then compares them against an approved list defined in a Level custom field (cf_dns
). If the script detects any DNS servers that aren’t on the allowed list or finds no DNS configuration at all, it raises an alert. When there’s a match with the approved settings, it reports success, confirming that the device is aligned with your network policies.
You can set up a script-based monitor in Level to run this check on-demand, ensuring immediate alerts when DNS discrepancies appear. Alternatively, incorporate it into a scheduled Level Automation to regularly audit DNS settings across your macOS fleet, automatically detecting and addressing any unauthorized changes.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-macos-monitor-dns-servers
# Configured expected DNS servers (comma-separated list)
expected_dns_servers_string="{{cf_dns}}"
# Convert the comma-separated list into an array
IFS=',' read -r -a expected_dns_servers <<< "$expected_dns_servers_string"
# Function to get current DNS servers
get_dns_servers() {
scutil --dns | grep 'nameserver\[[0-9]*\]' | awk '{print $NF}'
}
# Function to check DNS servers
check_dns_servers() {
local current_dns_servers=($(get_dns_servers))
echo "Allowed DNS servers: ${expected_dns_servers[*]}"
echo "Current DNS servers: ${current_dns_servers[*]}"
if [ ${#current_dns_servers[@]} -eq 0 ]; then
echo "ALERT: No DNS servers configured."
exit 1
fi
local match_found=0
for dns in "${current_dns_servers[@]}"; do
if [[ " ${expected_dns_servers[*]} " == *" $dns "* ]]; then
((match_found++))
fi
done
if [ $match_found -ne ${#current_dns_servers[@]} ]; then
echo "ALERT: Not all DNS servers are in the allowed list."
exit 1
else
echo "SUCCESS: DNS servers match the allowed list."
fi
}
# Check if the DNS servers match the allowed list
check_dns_servers
cf_dns
custom field updated with your authoritative DNS serverscf_dns
custom field in Level with a space-separated list of allowed DNS addresses.scutil
should work. Test in your specific environment to confirm compatibility.macOS Monitor - DNS Servers
This script, macOS DNS Server Check, retrieves the DNS servers configured on a macOS device and compares them against a predefined list of expected DNS servers. It alerts if no DNS servers are configured or if any current DNS servers fall outside the allowed list, helping maintain proper network security and configuration compliance.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.