Skip to content

Instantly share code, notes, and snippets.

@smj10j
Last active May 20, 2017 06:57
Show Gist options
  • Save smj10j/b72547d71f06e8a242ff43ee683b4e71 to your computer and use it in GitHub Desktop.
Save smj10j/b72547d71f06e8a242ff43ee683b4e71 to your computer and use it in GitHub Desktop.
Simple "One-liner" DNS-timing
#!/usr/bin/env bash
# set -x
##
## DNS timing "one-liner"
##
function _dns_timing() {
local EMAIL='59766160@opayq.com'
local HOSTS=( 'www.google.com' 'www.aol.com' 'www.reddit.com' 'netdna.bootstrapcdn.com' )
curl -s -o test-hosts.sh 'https://gist.githubusercontent.com/smj10j/b72547d71f06e8a242ff43ee683b4e71/raw/test-hosts.sh'
echo "==================================================" >> test-hosts.log
echo "============== $(date) ==============" >> test-hosts.log
bash test-hosts.sh "${HOSTS[@]}" 2>&1 | tee -a test-hosts.log
echo "==================================================" >> test-hosts.log
mail -s "DNS timing results from $(curl ifconfig.me) via $(whoami)@$(hostname)" ${EMAIL} < test-hosts.log
}
export _dns_timing
_dns_timing
#!/bin/bash
#set -x
declare -a HOSTS
declare -a TTLS
declare -a TTL_SUMS
HOSTS=("$@")
TTLS=( )
TTL_SUMS=( 0 0 )
N=50
NUM_HOSTS=${#HOSTS[@]}
function ttl() {
#echo "$(host -r -a -t a $1)" | awk '/^'$1'/ {print $2}' | head -1
echo "$(host -a -t a $1)" | awk '/^'$1'/ {print $2}' | head -1
}
function cleanup() {
# echo ""
# echo "TTLs:"
# echo "${TTLS[@]}"
# echo "TTL_SUMs:"
# echo "${TTL_SUMS[@]}"
# echo "HOSTs:"
# echo "${HOSTS[@]}"
echo ""
LEN=$((${#TTLS[@]} / NUM_HOSTS))
FS=$'\t'
INDEX=0;
echo "HOST${FS}TTL SUM${FS}NUM SAMPLES${FS}TTL AVG"
for host in ${HOSTS[@]}; do
echo "${HOSTS[INDEX]}${FS}${TTL_SUMS[INDEX]}${FS}$LEN${FS}"$((${TTL_SUMS[INDEX]} / LEN))
INDEX=$((INDEX+1))
done
}
trap cleanup EXIT
ITERATIONS=$((N*NUM_HOSTS))
echo "Iterations: $ITERATIONS"
echo "Num Hosts: $NUM_HOSTS"
for i in $(seq $ITERATIONS); do
INDEX=$((i%NUM_HOSTS))
HOST=${HOSTS[INDEX]}
DELAY=$(echo "$RANDOM*0.00005" | bc)
echo "Sleeping for ${DELAY}s..."
sleep $DELAY
TTL=$(ttl $HOST)
TTLS[$i]=$TTL
TTL_SUMS[$INDEX]=$((${TTL_SUMS[INDEX]}+TTL))
echo "TTL to resolve $HOST: $TTL"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment