Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Default values for options
- PING_IP="8.8.8.8" # Default ping IP address (Google DNS)
- DNS_CHECK_ADDRESS="github.com" # Default DNS check address
- INTERFACE="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 restored
- LOCK_FILE="/tmp/internet_monitor.lock" # Define the lock file path
- # Function to acquire the lock
- acquire_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 lock
- release_lock() {
- rm -f "$LOCK_FILE"
- }
- # Function to send a webhook notification
- send_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 connectivity
- check_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 resolution
- check_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 resolution
- main() {
- 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 information
- usage() {
- 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 getopts
- while 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 ;;
- esac
- done
- # Acquire the lock before starting the main loop
- acquire_lock
- # Run the main loop
- while true; do
- main
- sleep 1m # Adjust the time interval as needed for internet connectivity and DNS resolution checks
- done
- # Release the lock when the script exits
- release_lock
Add Comment
Please, Sign In to add comment