Last active
May 13, 2025 15:21
-
-
Save kohlerdominik/5289555936e6c36ccb01e3a0d1c0aae3 to your computer and use it in GitHub Desktop.
Log ping results of multiple ip-addresses to a csv file
This file contains hidden or 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
#!/bin/bash | |
# List of IP addresses to ping (one per line) | |
IPS=( | |
"1.1.1.1" | |
"192.168.1.1" | |
# Add more IPs here | |
) | |
# Ping interval in seconds | |
INTERVAL=5 | |
while true; do | |
# Get current time and calculate sleep duration until next interval mark | |
current_seconds=$(date +%s) | |
next_iteration=$((current_seconds - (current_seconds % INTERVAL) + INTERVAL)) | |
sleep $((next_iteration - current_seconds)) | |
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
current_date=$(date -u +"%Y-%m-%d") | |
log_file="ping_results_${current_date}.csv" | |
# Create file with headers if it doesn't exist | |
if [ ! -f "$log_file" ]; then | |
echo "timestamp;ip;response_ms;error" > "$log_file" | |
fi | |
# Start all pings in parallel | |
for ip in "${IPS[@]}"; do | |
( | |
result=$(ping -c 1 -W 4 "$ip" 2>&1) | |
if [ $? -eq 0 ]; then | |
response_time=$(echo "$result" | grep -oP 'time=\K[0-9.]+') | |
echo "$timestamp;$ip;$response_time;" >> "$log_file" | |
else | |
error=$(echo "$result" | sed -n 2,2p) | |
echo "$timestamp;$ip;;$error" >> "$log_file" | |
fi | |
) & | |
done | |
# Wait for all background processes to finish | |
wait | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment