Update dns_tester.sh

actual tests
This commit is contained in:
2025-09-15 12:13:05 +02:00
parent 662a4380a1
commit c23aaf493e

View File

@@ -75,12 +75,15 @@ test_dns_performance() {
local successful_tests=0 local successful_tests=0
local test_count=0 local test_count=0
# Get the first IPv4 DNS server for testing
local primary_dns=$(echo "$dns_servers" | cut -d',' -f1)
IFS=',' read -ra TARGET_ARRAY <<< "$targets" IFS=',' read -ra TARGET_ARRAY <<< "$targets"
for target in "${TARGET_ARRAY[@]}"; do for target in "${TARGET_ARRAY[@]}"; do
target=$(echo "$target" | xargs) target=$(echo "$target" | xargs)
if [[ -n "$target" ]]; then if [[ -n "$target" ]]; then
test_count=$((test_count + 1)) test_count=$((test_count + 1))
local latency=$(ping_dns_resolved "$target" "$dns_servers" 2>/dev/null || echo "999") local latency=$(measure_dns_query_time "$target" "$primary_dns" 2>/dev/null || echo "999")
if [[ "$latency" != "999" ]]; then if [[ "$latency" != "999" ]]; then
total_latency=$((total_latency + latency)) total_latency=$((total_latency + latency))
successful_tests=$((successful_tests + 1)) successful_tests=$((successful_tests + 1))
@@ -90,16 +93,7 @@ test_dns_performance() {
if [[ $successful_tests -gt 0 ]]; then if [[ $successful_tests -gt 0 ]]; then
local avg_latency=$((total_latency / successful_tests)) local avg_latency=$((total_latency / successful_tests))
local simulated_latency=$avg_latency RESULTS["$dns_name"]=$avg_latency
case $dns_name in
"Cloudflare") simulated_latency=$((avg_latency + 5)) ;;
"Google") simulated_latency=$((avg_latency + 8)) ;;
"Level3") simulated_latency=$((avg_latency + 12)) ;;
"OpenDNS") simulated_latency=$((avg_latency + 15)) ;;
"Quad9") simulated_latency=$((avg_latency + 18)) ;;
"Comodo") simulated_latency=$((avg_latency + 20)) ;;
esac
RESULTS["$dns_name"]=$simulated_latency
DNS_NAMES["$dns_name"]="$dns_servers" DNS_NAMES["$dns_name"]="$dns_servers"
else else
RESULTS["$dns_name"]=999 RESULTS["$dns_name"]=999
@@ -107,29 +101,50 @@ test_dns_performance() {
fi fi
} }
ping_dns_resolved() { measure_dns_query_time() {
local target=$1 local target=$1
local dns_servers=$2 local dns_server=$2
local best_latency=999 local latency=999
local fallback_ip=$(nslookup "$target" 2>/dev/null | grep "Address:" | grep -v "::" | tail -1 | awk '{print $2}') # Use dig to measure actual DNS query time
if [[ -z "$fallback_ip" ]]; then if command -v dig &> /dev/null; then
fallback_ip=$(nslookup "$target" 2>/dev/null | grep "Address:" | tail -1 | awk '{print $2}') # First verify the query works
fi local dig_output=$(timeout 5 dig @"$dns_server" "$target" A +short 2>/dev/null)
if [[ -n "$dig_output" ]]; then
# Measure the actual query time
local start_time=$(date +%s%3N)
timeout 5 dig @"$dns_server" "$target" A +short >/dev/null 2>&1
local end_time=$(date +%s%3N)
latency=$(( (end_time - start_time) ))
if [[ -n "$fallback_ip" ]]; then # Convert to milliseconds if needed
if [[ "$fallback_ip" == *":"* ]]; then if [[ $latency -gt 1000 ]]; then
local latency=$(ping6 -c 1 -W 2 "$fallback_ip" 2>/dev/null | grep "time=" | cut -d'=' -f2 | cut -d' ' -f1 | cut -d'.' -f1) latency=$(( latency / 1000 ))
else fi
local latency=$(ping -c 1 -W 2 "$fallback_ip" 2>/dev/null | grep "time=" | cut -d'=' -f2 | cut -d' ' -f1 | cut -d'.' -f1)
fi fi
if [[ -n "$latency" ]]; then elif command -v nslookup &> /dev/null; then
best_latency=$latency # Fallback to nslookup with timing
local start_time=$(date +%s%3N)
local nslookup_output=$(timeout 5 nslookup "$target" "$dns_server" 2>/dev/null)
local end_time=$(date +%s%3N)
if echo "$nslookup_output" | grep -q "Address:"; then
latency=$(( (end_time - start_time) ))
# Convert to milliseconds if needed
if [[ $latency -gt 1000 ]]; then
latency=$(( latency / 1000 ))
fi
fi fi
fi fi
echo "$best_latency" # Ensure latency is reasonable (not negative or too high)
if [[ $latency -lt 0 || $latency -gt 5000 ]]; then
latency=999
fi
echo "$latency"
} }
run_tests() { run_tests() {
@@ -235,20 +250,23 @@ show_post_results_menu() {
check_dependencies() { check_dependencies() {
local missing_deps=() local missing_deps=()
if ! command -v ping &> /dev/null; then if ! command -v dig &> /dev/null && ! command -v nslookup &> /dev/null; then
missing_deps+=("iputils-ping")
fi
if ! command -v nslookup &> /dev/null && ! command -v dig &> /dev/null; then
missing_deps+=("dnsutils or bind-utils") missing_deps+=("dnsutils or bind-utils")
fi fi
if [[ ${#missing_deps[@]} -gt 0 ]]; then if [[ ${#missing_deps[@]} -gt 0 ]]; then
echo -e "${RED}Missing dependencies: ${missing_deps[*]}${NC}" echo -e "${RED}Missing dependencies: ${missing_deps[*]}${NC}"
echo -e "${YELLOW}Please install them and run the script again.${NC}" echo -e "${YELLOW}Please install them and run the script again.${NC}"
echo -e "${CYAN}Example: sudo apt install dnsutils iputils-ping${NC}" echo -e "${CYAN}Example: sudo apt install dnsutils${NC}"
echo -e "${CYAN}Or: sudo yum install bind-utils${NC}"
exit 1 exit 1
fi fi
if command -v dig &> /dev/null; then
echo -e "${GREEN}Using dig for DNS queries (recommended)${NC}"
else
echo -e "${YELLOW}Using nslookup for DNS queries (dig preferred)${NC}"
fi
} }
main() { main() {
@@ -299,3 +317,4 @@ main() {
} }
main "$@" main "$@"