Skip to content

Instantly share code, notes, and snippets.

@cometkim
Last active May 5, 2024 04:05
Show Gist options
  • Save cometkim/b2717046ab45d8f158038c2b7eb9191e to your computer and use it in GitHub Desktop.
Save cometkim/b2717046ab45d8f158038c2b7eb9191e to your computer and use it in GitHub Desktop.
Healthcheck an anycast DNS
#!/bin/bash
function healthcheck() {
local domain=$1
local total_tests=$2
local total_failed_count=0
local ip_list=()
for ((i = 0; i < total_tests; i++)); do
# DNS may returns multiple records, skip CNAMEs, take only the first one.
local test_ip
test_ip=$(dig +short "$domain" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
echo "Testing $test_ip"
ip_list+=("$test_ip")
if ! ping -c 1 -W 2 "$test_ip" &> /dev/null; then
echo "Failed to connect: $test_ip"
total_failed_count=$((total_failed_count + 1))
fi
done
echo
echo "Total Tests: $total_tests"
echo "Total Failed Tests: $total_failed_count"
local failure_ratio
failure_ratio=$(echo "scale=3; $total_failed_count / $total_tests * 100" | bc)
printf "Failure Ratio: %.2f%%\n" "$failure_ratio"
echo
local unique_ip_list
unique_ip_list=($(printf "%s\n" "${ip_list[@]}" | sort -u))
echo "Tested IPs:"
for ip in "${unique_ip_list[@]}"; do
echo "- $ip"
done
}
healthcheck "vercel.com" 1000
#!/bin/bash
ip_to_int() {
local ip="$1"
local a b c d
IFS='.' read -r a b c d <<< "$ip"
echo "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
}
int_to_ip() {
local int="$1"
echo "$((int >> 24 & 255)).$((int >> 16 & 255)).$((int >> 8 & 255)).$((int & 255))"
}
start_int=$(ip_to_int "76.76.21.1")
end_int=$(ip_to_int "76.76.21.254")
for ((ip_int = start_int; ip_int <= end_int; ip_int++)); do
current_ip=$(int_to_ip "$ip_int")
if ! ping -c 1 -W 2 "$current_ip" &> /dev/null; then
echo "Failed: $current_ip"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment