EN
Bash - simple URL ping tool (URL response time measuring)
10
points
In this short article, we would like to show how in Bash, write a simple tool that pings indicated URLs checking response time.
test_requests.sh
file:
#!/bin/bash
function test_request()
{
url="$1"
t1=$(date +%s%9N)
curl -k "${url}" &> /dev/null
t2=$(date +%s%9N)
dt=$((t2 - t1))
printf "%16d %16d %16d %16d %s\n" \
$dt \
$(($dt / 1000)) \
$(($dt / 1000000)) \
$(($dt / 1000000000)) \
"$url";
}
echo
echo -n " [in nanoseconds] ";
echo -n "[in microseconds] ";
echo -n "[in milliseconds] ";
echo -n " [in seconds] ";
echo -n " [requested url] ";
echo
test_request "https://dirask.com/posts/MySQL-select-last-row-j890a1"
test_request "http://localhost/posts/MySQL-select-last-row-j890a1"
echo
Script running:
./test_requests.sh
# or:
watch -n 1 ./test_requests.sh
Example output:
Every 1.0s: ./test_requests.sh my-server: Sun May 23 13:00:56 2021
[in nanoseconds] [in microseconds] [in milliseconds] [in seconds] [requested url]
110754854 110754 110 0 https://dirask.com/posts/MySQL-select-last-row-j890a1
95801101 95801 95 0 http://localhost/posts/MySQL-select-last-row-j890a1
Custom Host:
header example
In this example, we are able to change Host:
header to the proper domain.
That configuration is useful when we have VirtualHost
s that redirect traffic depending on a specific domain.
test_requests.sh
file:
#!/bin/bash
function test_request()
{
url="$1"
host="$2"
t1=$(date +%s%9N)
if [ -n "$host" ];
then
curl -k --header "Host: $host" "$url" &> /dev/null
else
curl -k "$url" &> /dev/null
fi
t2=$(date +%s%9N)
dt=$((t2 - t1))
printf "%16d %16d %16d %16d %s" \
$dt \
$(($dt / 1000)) \
$(($dt / 1000000)) \
$(($dt / 1000000000)) \
"$url";
[ -n "$host" ] && echo -n " (Host: $host)";
echo
}
echo
echo -n " [in nanoseconds] ";
echo -n "[in microseconds] ";
echo -n "[in milliseconds] ";
echo -n " [in seconds] ";
echo -n " [requested url] ";
echo
test_request "https://dirask.com/posts/MySQL-select-last-row-j890a1" ""
test_request "https://localhost/posts/MySQL-select-last-row-j890a1" "dirask.com"
echo
Script running:
./test_requests.sh
# or:
watch -n 1 ./test_requests.sh
Example output:
Every 1.0s: ./test_requests.sh my-server: Sun May 23 13:00:56 2021
[in nanoseconds] [in microseconds] [in milliseconds] [in seconds] [requested url]
110754854 110754 110 0 https://dirask.com/posts/MySQL-select-last-row-j890a1
95801101 95801 95 0 https://localhost/posts/MySQL-select-last-row-j890a1 (Host: dirask.com)