this script is intended for long term testing of reliability of network connection/connectivity
it should quit after 24hours and thus not spamming the logs
the script:
#!/bin/bash
# $1 = ip to test
# $2 = how many hours to test
LOGFILE="test_connection_$1.log"
HOURS=$2
MINUTES=$(($2*60))
MIN_CURRENT=0
echo "====== connection test for $1 started on : $(date '+%Y-%m-%d-%H-%M')" > $LOGFILE;
echo "script is set to run for $HOURS h (= $MINUTES min)" >> $LOGFILE;
time for MIN_CURRENT in $(seq 1 $MINUTES);
do
ping -c 60 $1 >> $LOGFILE; # will ping every second thus one loop is 1 min
printf "\n--- MIN_CURRENT $MIN_CURRENT of $MINUTES --- timestamp: $(date '+%Y-%m-%d %H:%M:%S') \n" >> $LOGFILE;
done;
usage example:
# ping-test connection for 24hours and log it to file /scripts/test_connection.sh 192.168.0.223 24 # how to view progress? # open up a new terminal (Ctrl+Shift+N in GNU Debian Linux MATE) tail -f test_connection_192.168.0.223.log # how to generate nice summary, if any packet got lost cat test_connection_192.168.0.223.log |grep -e "MIN_CURRENT" -e "packet loss" 60 packets transmitted, 60 received, 0% packet loss, time 137ms --- MIN_CURRENT 1 of 1440 --- timestamp: 2021-01-21 11:35:57 60 packets transmitted, 60 received, 0% packet loss, time 142ms --- MIN_CURRENT 2 of 1440 --- timestamp: 2021-01-21 11:36:56 60 packets transmitted, 60 received, 0% packet loss, time 142ms --- MIN_CURRENT 3 of 1440 --- timestamp: 2021-01-21 11:37:55 # to terminate the script kill -SIGTERM $(pgrep test_connection)
possible improvements:
alternatively the log could be compressed afterwards to save disk space
tar fcvz $LOGFILE.tar.gz $LOGFILE;