Last active
April 10, 2024 19:28
-
-
Save SanariSan/8e5ad4a8823768f30d7408e3ab3af06f to your computer and use it in GitHub Desktop.
RTT for all hostname ips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Useful for checking rtt to any sotname of interest. | |
# Since servers usually have balancers or dns balancing is in place it is helpful to test every ip. | |
IPS=$(host $1 | grep "has address" | awk '{ print $4 }') | |
REQ_AMOUNT=${2:-5} | |
# Loop through each IP address | |
for IP in $IPS; do | |
# Get the city information for the IP address - take the second occurrence of "City:" | |
CITY=$(whois $IP | grep "City:" | head -2 | tail -1 | awk '{ print $2 }') | |
# Initialize total time | |
TOTAL_TIME=0 | |
# Repeat GET request 10 times | |
for ((i=0; i<$REQ_AMOUNT; i++)); do | |
# Perform GET request and measure time in seconds | |
TIME=$(curl -o /dev/null -s -w '%{time_total}\n' --connect-timeout 5 http://$IP) | |
echo "${TIME}ms" | |
# Add time to total time | |
TOTAL_TIME=$(echo "$TOTAL_TIME + $TIME" | bc) | |
done | |
# Calculate average time in seconds | |
AVG_TIME=$(echo "scale=2; $TOTAL_TIME / $REQ_AMOUNT" | bc) | |
# Convert average time to milliseconds | |
AVG_TIME_MS=$(echo "$AVG_TIME * 1000" | bc) | |
# Print the IP, its corresponding city, and the average response time | |
echo "IP: $IP, City: $CITY, Avg Time: ${AVG_TIME_MS}ms" | |
done | |
# USAGE: | |
# ./rt.sh hostname.com | |
# ./rt.sh hostname.com req_amount | |
# EXAMPLE: | |
# ./rt.sh google.com 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment