Created
July 19, 2024 06:34
-
-
Save hctilg/d11de5108ba2674012f3a4f9cadf23f6 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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