Checking host availability by using ping in bash scripts

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

    FYI, I just did some test using the method above and if we use multi ping (10 requests)

    ping -c10 8.8.8.8 &> /dev/null ; echo $?

    the result of multi ping command will be "0" if at least one of ping result reachable, and "1" in case where all ping requests are unreachable.

    0 讨论(0)
  • 2020-11-29 20:08
    for i in `cat Hostlist`
    do  
      ping -c1 -w2 $i | grep "PING" | awk '{print $2,$3}'
    done
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-29 20:18

    There is advanced version of ping - "fping", which gives possibility to define the timeout in milliseconds.

    #!/bin/bash
    IP='192.168.1.1'
    fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
    if [ "$?" = 0 ]
    then
      echo "Host found"
    else
      echo "Host not found"
    fi
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题