Skip to content

Instantly share code, notes, and snippets.

@thesuhu
Last active September 25, 2024 11:31
Show Gist options
  • Save thesuhu/4423916e2ae1ba7f23021f97045a6d60 to your computer and use it in GitHub Desktop.
Save thesuhu/4423916e2ae1ba7f23021f97045a6d60 to your computer and use it in GitHub Desktop.
This script tests the connection to a specified URL and provides detailed timing information.

Description

This script tests the connection to a specified URL and provides detailed timing information, useful for troubleshooting internet connectivity issues. It measures the time taken for various stages of the connection process, including:

Name lookup (DNS resolution) Connecting to the server Initiating data transfer Overall request completion This breakdown helps pinpoint potential bottlenecks in your network or the remote server.

Usage

./test_connection.sh <URL>
  • Replace script_name.sh with the actual name of your script file.
  • Replace with the website address you want to test the connection to.

Example

./test_connection.sh https://www.google.com
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <URL>"
exit 1
fi
URL="$1"
echo "Testing connection to $URL"
namelookup_time=$(curl -o /dev/null -s -w %{time_namelookup} $URL)
connect_time=$(curl -o /dev/null -s -w %{time_connect} $URL)
pretransfer_time=$(curl -o /dev/null -s -w %{time_pretransfer} $URL)
starttransfer_time=$(curl -o /dev/null -s -w %{time_starttransfer} $URL)
total_time=$(curl -o /dev/null -s -w %{time_total} $URL)
echo "Results:"
echo " Name lookup time: $namelookup_time seconds"
echo " Connect time: $connect_time seconds"
echo " Pretransfer time: $pretransfer_time seconds"
echo " Start transfer time: $starttransfer_time seconds"
echo " Total time: $total_time seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment