Last active
December 15, 2015 15:49
-
-
Save mortymacs/5284949 to your computer and use it in GitHub Desktop.
This is simple code for pinging an address and save the results in a log file and can get percent about lost and success packets and then run your command when you got lost packets over 20%!
This file contains 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 | |
#read -p "Enter your url: " $address | |
###### | |
# Ping URL and get percent | |
###### | |
address='google.com' | |
log_file='packet.log' | |
### ./ping.sh ping | |
if [ "$1" == "ping" ];then | |
check = $(ping $address -c1 > /dev/null 2>&1) | |
if [ "$check" -ne "0" ] | |
then | |
echo "2," >> $log_file | |
else | |
echo "1," >> $log_file | |
fi | |
fi | |
### ./ping.sh log "init 0" | |
### ./ping.sh log "mail ....." | |
if [ "$1" == "log" ];then | |
#lost | |
packet_false=$(grep 2 -o $log_file | wc -l) | |
#success | |
packet_true=$(grep 1 -o $log_file | wc -l) | |
#total | |
total_packet=$(($packet_false+$packet_true)) | |
p_f=$(($packet_false*100/$total_packet)) | |
p_t=$(($packet_true*100/$total_packet)) | |
echo "packet lost:" $p_f% | |
echo "Packet Success:" $p_t% | |
if [ "$p_f" -gt "20" ] | |
then | |
$2 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can run it via Cron . For example ping every 10 minutes and check log file after 2 hours if the lost packets were over that 20% , then turn off the system.
10 * * * * /your/path/ping.sh ping