R
R
Roman Bolshukhin2021-08-10 12:39:17
bash
Roman Bolshukhin, 2021-08-10 12:39:17

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

2 answer(s)
S
Saboteur, 2021-08-10
@saboteur_kiev

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 $?

After discussion in the comments, here is a more useful script:
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

A
Alexey Dmitriev, 2021-08-10
@SignFinder

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 question

Ask a Question

731 491 924 answers to any question