Checking host availability by using ping in bash scripts

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

    I can think of a one liner like this to run

    ping -c 1 127.0.0.1 &> /dev/null && echo success || echo fail
    

    Replace 127.0.0.1 with IP or hostname, replace echo commands with what needs to be done in either case.

    Code above will succeed, maybe try with an IP or hostname you know that is not accessible.

    Like this:

    ping -c 1 google.com &> /dev/null && echo success || echo fail
    

    and this

    ping -c 1 lolcatz.ninja &> /dev/null && echo success || echo fail
    

提交回复
热议问题