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
[ $PONG =~ ^[0-9] ]and write that lines to /dev/null could be an option.