#!/bin/bash set -x # Function to get the default gateway IP address get_gateway_ip() { gateway_ip=$(ip route | awk '/default/ {print $3}') echo "$gateway_ip" } # Array of IP addresses to ping, including the gateway IP ip_addresses=("9.9.9.9" "8.8.8.8" "1.1.1.1") gateway_ip=$(get_gateway_ip) if [ -n "$gateway_ip" ]; then ip_addresses+=("$gateway_ip") fi # CSV file to store ping results csv_file="ping_data.csv" # Check if the CSV file already exists if [ ! -e "$csv_file" ]; then # Create the CSV file with a header including a timestamp column echo "Timestamp,IP Address,Response Time (ms),Status" > "$csv_file" fi # Function to ping an IP address and log the result to the CSV file ping_ip() { ip="$1" result=$(ping -c 4 -i 0.2 -q "$ip" | tail -1) if [ $? -eq 0 ]; then response_time=$(echo "$result" | awk -F'/' '{print $5}') status="Success" else response_time="N/A" status="Failed" fi timestamp=$(date +"%Y-%m-%d %H:%M:%S") echo "$timestamp,$ip,$response_time,$status" >> "$csv_file" } # Loop through the IP addresses and ping them periodically while true; do for ip in "${ip_addresses[@]}"; do ping_ip "$ip" done sleep 60 # Ping every 60 seconds, adjust as needed done