./internet_monitor.sh -p 1.1.1.1 -d example.com
#!/bin/bash# Default values for optionsPING_IP="8.8.8.8" # Default ping IP address (Google DNS)DNS_CHECK_ADDRESS="github.com" # Default DNS check addressINTERFACE="eth0" # Replace with your Ethernet interface name (e.g., eth0)WEBHOOK_URLS="YOUR_WEBHOOK_URL" # Default webhook URL(s) (comma-separated)ENABLE_RESTORE_NOTIFICATION=true # Set this to false if you want to disable the notification when connectivity is restoredLOCK_FILE="/tmp/internet_monitor.lock" # Define the lock file path# Function to acquire the lockacquire_lock() { if [[ -e "$LOCK_FILE" ]]; then echo "Lock file exists. Script is already running or a previous run didn't finish." exit 1 fi touch "$LOCK_FILE"}# Function to release the lockrelease_lock() { rm -f "$LOCK_FILE"}# Function to send a webhook notificationsend_webhook_notification() { local message="$1" local urls=($(echo "$WEBHOOK_URLS" | tr ',' ' ')) # Convert comma-separated URLs to an array for url in "${urls[@]}"; do curl -X POST -H "Content-Type: application/json" -d "{\"text\":\"$message\"}" "$url" done}# Function to check internet connectivitycheck_internet_connectivity() { local ip_address="$1" if ping -I "$INTERFACE" -c 1 "$ip_address" &> /dev/null; then return 0 # Internet connectivity is available else return 1 # Internet connectivity is not available fi}# Function to check DNS resolutioncheck_dns_resolution() { local dns_address="$1" if ping -I "$INTERFACE" -c 1 "$dns_address" &> /dev/null; then return 0 # DNS resolution is successful else return 1 # DNS resolution failed fi}# Main function to monitor internet connectivity and DNS resolutionmain() { if ! check_internet_connectivity "$PING_IP"; then # Internet connectivity is not available, send webhook notification with a specific payload message="Internet connectivity is down on $INTERFACE at $(date)" send_webhook_notification "$message" "connectivity_down" elif ! check_dns_resolution "$DNS_CHECK_ADDRESS"; then # DNS resolution failed, send webhook notification message="DNS resolution failed on $INTERFACE at $(date)" send_webhook_notification "$message" "dns_failure" else # Internet connectivity and DNS resolution are both successful if $ENABLE_RESTORE_NOTIFICATION; then # Internet connectivity is restored, send webhook notification with a specific payload message="Internet connectivity restored on $INTERFACE at $(date)" send_webhook_notification "$message" "connectivity_restored" fi fi}# Function to display usage informationusage() { echo "Usage: $0 [-p ping_ip_address] [-d dns_check_address] [-w webhook_urls]" echo "Options:" echo " -p ping_ip_address Specify the IP address for internet connectivity check (default: 8.8.8.8)" echo " -d dns_check_address Specify the DNS address for DNS resolution check (default: github.com)" echo " -w webhook_urls Specify one or multiple webhook URLs as a comma-separated list" exit 1}# Parse command-line options using getoptswhile getopts ":p:d:w:" opt; do case "$opt" in p) PING_IP=$OPTARG ;; d) DNS_CHECK_ADDRESS=$OPTARG ;; w) WEBHOOK_URLS=$OPTARG ;; \?) echo "Invalid option: -$OPTARG" >&2 ; usage ;; :) echo "Option -$OPTARG requires an argument." >&2 ; usage ;; esacdone# Acquire the lock before starting the main loopacquire_lock# Run the main loopwhile true; do main sleep 1m # Adjust the time interval as needed for internet connectivity and DNS resolution checksdone# Release the lock when the script exitsrelease_lock