0

I just made this script for host monitoring.

The goal is to ping the host and log the output to a file but also to show output to the terminal. If the host is unresponsive it will log the time with the appropriate error message. The goal is to log the time-stamp of the up/down times.

#!/bin/bash

DATE=$(date +"%d.%m.%Y %T")
SHORT_DATE=$(date +"%d.%m.%Y")

echo
echo "Pinging host " $@
echo

HOST=$@


ping $HOST | while read PONG
do
        grep ttl <<< "$PONG"

        if [ $? -eq 0 ]; then

                echo "`date`: $PONG"
                echo "`date`: $PONG" &>> ping_check_$SHORT_DATE.log

        else

                echo "`date`: ping failed, $HOST host is DOWN!" &>> ping_check_$SHORT_DATE.log
                echo "$PONG" &>> ping_check_$SHORT_DATE.log
        fi
done

Now the problem is that when the ping is successful the output from the script looks like this (I get two lines of output, and I don't need the first line):

[spirit@vas scripts]$ ./ping_check3.sh 10.10.0.254

Pinging host  10.10.0.254

64 bytes from 10.10.0.254: icmp_seq=1 ttl=255 time=1.18 ms
Wed Jun  3 10:35:52 CEST 2015: 64 bytes from 10.10.0.254: icmp_seq=1 ttl=255 time=1.18 ms
64 bytes from 10.10.0.254: icmp_seq=2 ttl=255 time=1.28 ms
Wed Jun  3 10:35:53 CEST 2015: 64 bytes from 10.10.0.254: icmp_seq=2 ttl=255 time=1.28 ms
64 bytes from 10.10.0.254: icmp_seq=3 ttl=255 time=1.34 ms

The above output is from the TERMINAL. I need only the result with the timestamp and not the first line which comes from the ping $HOST itself?:

Wed Jun  3 10:35:52 CEST 2015: 64 bytes from 10.10.0.254: icmp_seq=1 ttl=255 time=1.18 ms

How can I fix the script so that only the line with the timestamp is shown at the output?


EDIT:

Just for clarification:

The output from the logfile looks like this:

Wed Jun  3 10:35:52 CEST 2015: 64 bytes from 10.10.0.254: icmp_seq=1 ttl=255 time=1.18 ms
Wed Jun  3 10:35:53 CEST 2015: 64 bytes from 10.10.0.254: icmp_seq=2 ttl=255 time=1.28 ms
Wed Jun  3 10:35:54 CEST 2015: 64 bytes from 10.10.0.254: icmp_seq=3 ttl=255 time=1.34 ms
Wed Jun  3 10:35:55 CEST 2015: 64 bytes from 10.10.0.254: icmp_seq=4 ttl=255 time=1.58 ms
  • What is the "output": the terminal, or the log file? – Volker Siegel Jun 3 '15 at 8:54
  • using regex with an if-statement like [ $PONG =~ ^[0-9] ] and write that lines to /dev/null could be an option. – quenia Jun 3 '15 at 9:00
  • @VolkerSiegel The first output if from the terminal. The output from the log-file does not have the extra output as the one from the terminal.. I also edited my OP and added a clarification. – Spirit Jun 3 '15 at 9:01
2

Just edit:

grep ttl <<< "$PONG"

To:

grep -q ttl <<< "$PONG"

The output you get is from grep. With the -q flag grep is quiet.

  • Hey thanks, that did it. I didn't realized that the output could've come from the grep, since that line is to check if the ping has ttl inside that means it is successful. – Spirit Jun 3 '15 at 9:10

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.