Checking host availability by using ping in bash scripts

前端 未结 8 2121
-上瘾入骨i
-上瘾入骨i 2020-11-29 20:07

I want to write a script, that would keep checking if any of the devices in network, that should be online all day long, are really online. I tried to use ping, but

相关标签:
8条回答
  • 2020-11-29 20:24

    Ping returns different exit codes depending on the type of error.

    ping 256.256.256.256 ; echo $?
    # 68
    
    ping -c 1 127.0.0.1 ; echo $?
    # 0
    
    ping -c 1 192.168.1.5 ; echo $?
    # 2
    

    0 means host reachable

    2 means unreachable

    0 讨论(0)
  • 2020-11-29 20:30
    up=`fping -r 1 $1 `
    if [ -z "${up}" ]; then
        printf "Host $1 not responding to ping   \n"
        else
        printf "Host $1 responding to ping  \n"
    fi
    
    0 讨论(0)
提交回复
热议问题