Checking host availability by using ping in bash scripts

前端 未结 8 2146
-上瘾入骨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:13

    You don't need the backticks in the if statement. You can use this check

    if ping -c 1 some_ip_here &> /dev/null
    then
      echo 1
    else
      echo 0
    fi
    

    The if command checks the exit code of the following command (the ping). If the exit code is zero (which means that the command exited successfully) the then block will be executed. If it return a non-zero exit code, then the else block will be executed.

提交回复
热议问题