Skip to content

Instantly share code, notes, and snippets.

@ibrahimhajjaj
Created January 25, 2024 12:14
Show Gist options
  • Select an option

  • Save ibrahimhajjaj/77a36ee163b3eef368c9a4d8fbba3c58 to your computer and use it in GitHub Desktop.

Select an option

Save ibrahimhajjaj/77a36ee163b3eef368c9a4d8fbba3c58 to your computer and use it in GitHub Desktop.
A handy Bash script to monitor your internet connection, log downtimes, and notify you of changes in connectivity status.
#!/bin/bash
# Default Configurations
DEFAULT_PING_HOSTS=("8.8.8.8" "1.1.1.1")
DEFAULT_CHECK_INTERVAL=10
DEFAULT_LOG_FILE="$(dirname "$0")/internet_downtime.log"
# Initialize Configurations
PING_HOSTS=("${DEFAULT_PING_HOSTS[@]}")
CHECK_INTERVAL=$DEFAULT_CHECK_INTERVAL
LOG_FILE=$DEFAULT_LOG_FILE
# Parse Command Line Arguments
while getopts "h:i:l:" opt; do
case $opt in
h) IFS=',' read -r -a PING_HOSTS <<< "$OPTARG";;
i) CHECK_INTERVAL="$OPTARG";;
l) LOG_FILE="$OPTARG";;
*) echo "Usage: $0 [-h hosts] [-i interval] [-l logfile]"; exit 1;;
esac
done
# Function Definitions
check_internet() {
for host in "${PING_HOSTS[@]}"; do
if /sbin/ping -c 1 "$host" &>/dev/null; then
return 0
fi
done
return 1
}
log_message() {
echo "$(date) - $1" | tee -a "$LOG_FILE"
}
send_notification() {
local message=$1
if [ "$(uname)" == "Darwin" ]; then
/usr/bin/osascript -e "Display notification \"$message\" with title \"Connectivity Alert\""
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
notify-send "Connectivity Alert" "$message"
fi
}
handle_exit() {
log_message "Internet connectivity checker stopped."
exit 0
}
# Trap SIGINT for graceful termination
trap handle_exit SIGINT
# Main Loop
log_message "Internet connectivity checker started."
notification_sent=0
down_times=0
longest_duration=0
downtime_start=0
while true; do
if check_internet; then
if [ "$notification_sent" -eq 1 ]; then
send_notification "Internet is reconnected"
notification_sent=0
downtime_end=$(date +%s)
duration=$((downtime_end - downtime_start))
down_times=$((down_times + 1))
longest_duration=$(($duration > $longest_duration ? $duration : $longest_duration))
fi
echo -ne "\rDowntimes: $down_times | Longest Duration: ${longest_duration}s | Status: \033[32mONLINE\033[0m"
else
if [ "$notification_sent" -eq 0 ]; then
send_notification "Internet is disconnected"
notification_sent=1
downtime_start=$(date +%s)
log_message "Internet down"
fi
echo -ne "\rDowntimes: $down_times | Longest Duration: ${longest_duration}s | Status: \033[31mOFFLINE\033[0m"
fi
sleep "$CHECK_INTERVAL"
done
@ibrahimhajjaj
Copy link
Author

Internet Connectivity Checker

Overview

The Internet Connectivity Checker is a Bash script designed to monitor internet connectivity. It logs downtimes, notifies the user when the internet status changes, and is equipped with customization options for different use cases. This tool is ideal for network troubleshooting and continuous monitoring of your internet connection.

Features

  • Connectivity Monitoring: Regularly pings configured hosts to check internet connectivity.
  • Downtime Logging: Records each downtime incident with timestamps.
  • Notifications: Sends desktop notifications when the internet status changes.
  • Customizable: Allows setting custom hosts and check intervals via command-line arguments.
  • Cross-Platform Notifications: Supports macOS and Linux desktop notifications.
  • Color-coded Status: Displays a real-time, color-coded connectivity status in the terminal.

Usage

To use the script, you can either run it with its default settings or provide optional command-line arguments for customization.

Running with Default Settings

Simply execute the script:

./internet_connectivity_checker.sh

Custom Usage

To run the script with custom settings:

./internet_connectivity_checker.sh -h "8.8.8.8,1.1.1.1" -i 5 -l /path/to/your/logfile.log
  • -h: Comma-separated list of hosts to ping (default: "8.8.8.8,1.1.1.1").
  • -i: Interval in seconds between checks (default: 10).
  • -l: Path to the log file (default: same directory as the script).

Requirements

This script requires Bash and utilizes common utilities (ping, date, etc.) available in most Unix-like environments. For notifications:

  • macOS: Requires osascript.
  • Linux: Requires notify-send.

License

This project is open source and available under the MIT License.

Contributions

Contributions, issues, and feature requests are welcome!.


Happy monitoring! Stay connected. 🌐

@Christian-Delair
Copy link

Hello Ibrahim,

I have integrated your Internet Connectivity Checker script on my machine.
I first tested it on Linux-Mint, then I use it now on a CentOS machine.
I found just a very small problem on these two OS.

On check_internet() you use /sbin/ping.
But the default right path for theses two OS is /usr/bin/ping

Thank you very much for this very good and well-built tool.

--
Chris

@petesoper
Copy link

Chat GPT5 sent me here when I asked for "best Linux script for monitoring internet connection". You might imagine my surprise when I ran it on Ubuntu 20.04 and it reported the connection was down and never changed its opinion. I'm in the "extremely skeptical " camp in regards to LLMs, so this was another juicy data point.

Then I scrolled down and read the note from Chris above. As you haven't acted on this in the three weeks since Chris posted, I guess your attention is elsewhere. But IMO you are famous.

As Chris implied, the wrong path is a trivial problem to correct and I'm proceeding to add uplink and downlink quality judgments and appreciate the help your code has provided.
Best Regards,
Pete

@petesoper
Copy link

Unfortunately the for loop doing pings only ever uses the first host.

@petesoper
Copy link

This will be the last note. This script gives the appearance of working by happy accident. Function return values are not evaluated properly. You don't test the function by name after a call, you test $?. The value of the function name as a right hand side is itself (the spelling of the function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment