summaryrefslogtreecommitdiffstats
path: root/src/ping_check.sh
blob: 77ee7d79f398e8caf680e4e40b1dc186afbb1c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
##############################################################
#  Copyright (C) 2004 by Christoph Thielecke
#  crissi99@gmx.de
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the
#  Free Software Foundation, Inc.,
#  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
#
# checks if the ping answer was sucessful within given times
#
# syntax: ping_check.sh <ping cmd> <device> <ping host ip> <interval in sec> <test ping count> <verbose>
# example: ping_check.sh  
# device can be also default then no device is supplied to ping
#
##############################################################

if [ $# -ne 6 ]; then
echo "help:"
echo "-----"
echo "PING <path to ping>"
echo "DEVICE <device>"
echo "PINGHOST <ip | hostname>"
echo "INTERVAL <val>"
echo "TEST_PING_COUNT <val>"
echo "QUIET [0|1]"
echo ""
echo "parameters count: $#"
exit 1
fi

PING=$1
DEVICE=$2
PINGHOST=$3
INTERVAL=$4
TEST_PING_COUNT=$5
QUIET=$6

DEVICECMD=""

if [ $DEVICE != "default" ]; then
	DEVICECMD="-I $DEVICE"
fi

if [  $QUIET -eq '1' ]; then
echo "Configuration:"
echo "Host: "$PINGHOST
echo "Device: "$DEVICE
echo "PING count: "$TEST_PING_COUNT
echo "Interval: "$INTERVAL
echo "- - - - -"
echo
fi

while true; do
fails=0
count=0
while [ $count -lt $TEST_PING_COUNT ]; do
	if [  $QUIET -eq '1' ]; then
		echo -n "Ping sequence "$count": "
	fi
	if [ "x" = "x$(ping -c 1 -w 5 $DEVICECMD $PINGHOST 2>&1 | grep '1 received')" ]; then
		fails=`expr $fails + 1`
		if [  $QUIET -eq '1' ]; then
			echo "failed!"
		fi
	else
		if [  $QUIET -eq '1' ]; then
			echo "ok."
		fi
	fi
	count=`expr $count + 1`
	sleep $INTERVAL
	done
	
	if [  $QUIET -eq '1' ]; then
	echo -n "PING failitures: "$fails" => "
	fi
	if [ $fails -gt $(expr $TEST_PING_COUNT - 1) ]; then
			echo "PING failed!"
	else
			echo "PING ok."
	fi
done