User Tools

Site Tools


howtos:dns_server_latency_script

DNS Latency

Example 1

The script calls two dns servers directly (the ones you want to measure) and returns the query latency.

while true
do
	ns2=$(dig -4 @1.1.1.1 example.com | grep Query | cut -d \: -f 2 | sed -e 's/ //' ) 
	ns3=$(dig -4 @1.0.0.1 other-example.com | grep Query | cut -d \: -f 2 | sed -e 's/ //') 
	echo "ns2 latency: $ns2 / ns3 latency: $ns3"; sleep $(($RANDOM% 10 + 1))
done

Example 2

The script will pick three random name from the list and request the corresponding type.

#!/bin/bash

# Set the list of DNS names to test
dns_names=(
  "example.com A"
  "example.net MX"
  "example.org NS"
  "example.info TXT"
  "example.edu SPF"
  "example.gov SRV"
  "example.net AAAA"
  "example.org CNAME"
  "example.info PTR"
  "example.edu SOA"
)

# Set the list of DNS servers to test
dns_servers=(8.8.8.8 8.8.4.4 1.1.1.1 1.0.0.1)

# Randomly shuffle the list of DNS names
num_names=${#dns_names[@]}
for ((i=0; i<num_names; i++)); do
  # Generate a random index between 0 and the number of names - 1
  j=$((RANDOM % (num_names - i)))

  # Swap the name at index i with the name at index j
  temp=${dns_names[i]}
  dns_names[i]=${dns_names[i+j]}
  dns_names[i+j]=$temp
done

# Select the first three DNS names from the shuffled list
dns_names=("${dns_names[@]:0:3}")

# Iterate over the list of DNS names
for name in "${dns_names[@]}"; do
  # Split the name and type into separate variables
  read dns_name dns_type <<< "$name"

  # Iterate over the list of DNS servers
  for server in "${dns_servers[@]}"; do
    # Use the `dig` command to measure the latency of the DNS server for the name and type
    latency=$(dig +tries=1 +time=2 +stats @"$server" "$dns_name" "$dns_type" | grep Query | awk '{print $4 " ms"}')
    # Print the results
    printf "%s %s %s %s\n" "$server" "$dns_name" "$dns_type" "$latency"
  done
done

Example 3

This script will pick two domain names from the list and loop over them “num_loops” of times with a random sleep in between.

#!/bin/bash

# Set the number of loops to run
num_loops=5

# Set the list of DNS servers to test
dns_servers=(8.8.8.8 8.8.4.4 1.1.1.1 1.0.0.1)

# Set the list of DNS names to resolve
dns_names=(www.google.com www.facebook.com www.twitter.com www.instagram.com www.reddit.com)

# Iterate over the number of loops
for ((i=1; i<=num_loops; i++)); do
  # Pick two random names from the list
  name1=$((RANDOM % ${#dns_names[@]}))
  name2=$((RANDOM % ${#dns_names[@]}))

  # Iterate over the list of DNS servers
  for server in "${dns_servers[@]}"; do
    # Use the `dig` command to measure the latency of the DNS server for the two random names
    latency1=$(dig +tries=1 +time=2 +stats @"$server" "${dns_names[$name1]}" | grep Query | awk '{print $4 " ms"}')
    latency2=$(dig +tries=1 +time=2 +stats @"$server" "${dns_names[$name2]}" | grep Query | awk '{print $4 " ms"}')
    # Print the results
    printf "%s %s %s %s %s\n" "$server" "${dns_names[$name1]}" "$latency1" "${dns_names[$name2]}" "$latency2"
  done

  # Sleep for a random number of seconds between 1 and 10
  sleep $(($RANDOM % 10 + 1))
done

Example 4

This is a rather dubious script. It will pick two base domains from the list, three record types from the list and generate two random hostnames. It then glues it all together and iterate over the different records.

#!/bin/bash

# Set the base domains
base_domains=(example.com example.org example.net)

# Set the list of DNS record types to test
record_types=(A AAAA CNAME MX NS PTR SOA SPF SRV TXT)

# Set the number of loops to run
num_loops=5

# Set the list of DNS servers to test
dns_servers=(8.8.8.8 8.8.4.4 1.1.1.1 1.0.0.1)

# Iterate over the number of loops
for ((i=1; i<=num_loops; i++)); do
  # Pick two random base domains from the list
  domain1=${base_domains[$((RANDOM % ${#base_domains[@]}))]}
  domain2=${base_domains[$((RANDOM % ${#base_domains[@]}))]}

  # Generate two random DNS names based on the base domains
  name1=$(tr -dc 'a-z0-9' < /dev/urandom | head -c 8).$domain1
  name2=$(tr -dc 'a-z0-9' < /dev/urandom | head -c 8).$domain2

  # Iterate over the list of DNS servers
  for server in "${dns_servers[@]}"; do
    # Pick three random record types from the list
    type1=${record_types[$((RANDOM % ${#record_types[@]}))]}
    type2=${record_types[$((RANDOM % ${#record_types[@]}))]}
    type3=${record_types[$((RANDOM % ${#record_types[@]}))]}

    # Use the `dig` command to measure the latency of the DNS server for the two random names and the three random record types
    latency1=$(dig +tries=1 +time=2 +stats @"$server" "$name1" "$type1" | grep Query | awk '{print $4 " ms"}')
    latency2=$(dig +tries=1 +time=2 +stats @"$server" "$name2" "$type2" | grep Query | awk '{print $4 " ms"}')
    latency3=$(dig +tries=1 +time=2 +stats @"$server" "$name1" "$type3" | grep Query | awk '{print $4 " ms"}')
    latency4=$(dig +tries=1 +time=2 +stats @"$server" "$name2" "$type3" | grep Query | awk '{print $4 " ms"}')
    # Print the results
    printf "%s %s %s %s %s %s %s %s\n" "$server" "$name1" "$type1" "$latency1" "$name2" "$type2" "$latency2" "$type3" "$latency3" "$latency4"
  done

  # Sleep for a random number of seconds between 1 and 10
  sleep $(($RANDOM % 10 + 1))
done

Example 5

This version is similar to example 2 but instead of using dig it uses host.

#!/bin/bash

# Set the list of DNS names to test
dns_names=(
  "example.com A"
  "example.net MX"
  "example.org NS"
  "example.info TXT"
  "example.edu SPF"
  "example.gov SRV"
  "example.net AAAA"
  "example.org CNAME"
  "example.info PTR"
  "example.edu SOA"
)

# Set the list of DNS servers to test
dns_servers=(8.8.8.8 8.8.4.4 1.1.1.1 1.0.0.1)

# Randomly shuffle the list of DNS names
num_names=${#dns_names[@]}
for ((i=0; i<num_names; i++)); do
  # Generate a random index between 0 and the number of names - 1
  j=$((RANDOM % (num_names - i)))

  # Swap the name at index i with the name at index j
  temp=${dns_names[i]}
  dns_names[i]=${dns_names[i+j]}
  dns_names[i+j]=$temp
done

# Select the first three DNS names from the shuffled list
dns_names=("${dns_names[@]:0:3}")

# Iterate over the list of DNS names
for name in "${dns_names[@]}"; do
  # Split the name and type into separate variables
  dns_name=${name% *}
  dns_type=${name#* }

  # Iterate over the list of DNS servers
  for server in "${dns_servers[@]}"; do
    # Use the `host` command to measure the latency of the DNS server for the name and type
    latency=$( (time host -W 2 "$dns_name" "$server") 2>&1 | grep real | awk '{print $2}')
    # Print the results
    printf "%s %s %s %s\n" "$server" "$dns_name" "$dns_type" "$latency"
  done
done
howtos/dns_server_latency_script.txt · Last modified: d/m/Y H:i by domingo