-1

I'm trying to diagnose network instability on my home LAN -- I'm losing connectivity between my router and my cable modem. In my shell script, I'd like to ping the first ip address past the modem, and return false when that fails, so that I can trigger other actions in my script.

Does anyone know of such a beast?

5

Yes, it is called ping.

Try something like this (in bash):

while true ; do 
  if ping -c 1 10.202.15.54 > /dev/null ; then 
    echo "It works"
  else 
    echo "It does not work"
  fi
  sleep 5
done
0
ping -c 1 $my_host > /dev/null ; if [ $? -eq 0 ]; then "echo all good";else "echo error"
0

I'd do this, you'd get a log so you could run it over time and graph the result in excel.

#!/bin/bash
#Script to ping the IP after the gateway. 

LOG="ping.csv"
TARGET="10.0.0.1"

while :
do
    if  `ping -c1 $TARGET > /dev/null ` 
    then
        STATUS="OK"
    else    
        STATUS="FAIL"
    fi

    echo "`date +'%D %H:%M:%S'` : $STATUS "
    echo "`date +'%D %H:%M:%S'` , $STATUS " >> $LOG
    sleep 30
done
-1

Pretty simple:

$ ping -c 1 -W 3 www.go.org | grep -q "100% packet loss" && echo "Packet loss" || echo "Packet received"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.