Answer the question
In order to leave comments, you need to log in
How to ping in BASH?
Good afternoon. How to make a network ping script in such a way that conditionally assign to a variable a connection break if it lasts more than 1 minute and connection restoration if there was a break before? Thanks
Answer the question
In order to leave comments, you need to log in
Ping with one-time pings and check the return code in the $? variable: 0 success, not 0 - communication error.
ping mysite.com -c 1
echo $?
declare -i failcount=0
status="OK"
while true: do
if ping mysite.com -c 1; then
if [ "$status" == "FAIL" ]; then
echo "Network is restored"
curl -X GET -k "https://api.telegram.org/bot1938&text=Connection_is_restored";
status="OK"
fi
failcount=0
else
failcount+=1
if [ "$status" == "OK" ] && [ $failcount -gt 60 ] ; then
echo "Network is unavailable for 60 seconds!!!"
curl -X GET -k "https://api.telegram.org/bot1938&text=Connection_broken_for_60_seconds";
status="FAIL"
fi
fi
done
You need to go to google, drive in the request "bash if ping successful", find the one you need and adapt it to your liking.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question