Skip to content

Instantly share code, notes, and snippets.

@hctilg
Created July 19, 2024 06:34
Show Gist options
  • Save hctilg/d11de5108ba2674012f3a4f9cadf23f6 to your computer and use it in GitHub Desktop.
Save hctilg/d11de5108ba2674012f3a4f9cadf23f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Counter for connection outages
connection_outage_count=0
# Function to handle interruption by Ctrl-C
function cleanup {
echo "Script interrupted."
echo "Number of connection outages: $connection_outage_count"
exit 1
}
trap cleanup SIGINT
while true; do
# Use the ping command to check the response time
ping -c 1 8.8.8.8 > /tmp/ping_result
if [ $? -ne 0 ]; then
# If ping command returns non-zero status (indicating an error), increment outage count
((connection_outage_count++))
echo "πŸ’©πŸ’©πŸ’©πŸ’©πŸ’© Connection outage (Count: $connection_outage_count)"
else
response_time=$(grep "time=" /tmp/ping_result | cut -d' ' -f7 | cut -d'=' -f2)
if [ -z "$response_time" ]; then
# If response_time is empty, it means a timeout occurred
((connection_outage_count++))
echo "πŸ’©πŸ’©πŸ’©πŸ’©πŸ’© Connection outage (Count: $connection_outage_count)"
elif (( $(echo "$response_time > 250" | bc -l) )); then
# If response_time is greater than 250 ms, print "Bad connection" message
echo "πŸ”΄πŸ”΄πŸ”΄πŸ”΄πŸ”΄ Bad connection - Response time: ${response_time} ms"
else
# If response_time is less than or equal to 250 ms, print "Normal connection" message
echo "🟒🟒🟒🟒🟒 Normal connection - Response time: ${response_time} ms"
fi
fi
# Sleep for 5 seconds before the next iteration
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment