Checking host availability by using ping in bash scripts

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

    This seems to work moderately well in a terminal emulator window. It loops until there's a connection then stops.

    #!/bin/bash
    
    # ping in a loop until the net is up
    
    declare -i s=0
    declare -i m=0
    while ! ping -c1 -w2 8.8.8.8 &> /dev/null ;
    do
      echo "down" $m:$s
      sleep 10
      s=s+10
      if test $s -ge 60; then
        s=0
        m=m+1;
      fi
    done
    echo -e "--------->>  UP! (connect a speaker) <<--------" \\a
    

    The \a at the end is trying to get a bel char on connect. I've been trying to do this in LXDE/lxpanel but everything halts until I have a network connection again. Having a time started out as a progress indicator because if you look at a window with just "down" on every line you can't even tell it's moving.

提交回复
热议问题