-
Notifications
You must be signed in to change notification settings - Fork 3
/
ping-til-alive.sh
executable file
·106 lines (85 loc) · 1.62 KB
/
ping-til-alive.sh
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
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# exit on error
set -e
# Usage
function usage {
cat << EOF
usage: $0 [options] IP/HOSTNAME
Wait for a network device to respond.
OPTIONS:
-h Help
-r Ignore inability to resolve hostnames
-t Timeout in seconds (0 for no timeout, default: $DEFAULT_TIMEOUT)
-v Verbose
EOF
}
# If verbose is enabled echo the given message
function v {
if [ $verbose -ne 0 ]
then
echo $*
fi
}
# Parse options and args
DEFAULT_TIMEOUT=10
host=
timeout=$DEFAULT_TIMEOUT
verbose=0
while getopts "ht:v" option
do
case $option in
h)
usage
exit
;;
t)
timeout=$OPTARG
;;
v)
verbose=1
;;
esac
done
shift $(($OPTIND - 1))
host=$1
if [ -z $host ]
then
echo "A host is required"
usage
exit 1
fi
# Stores the result of ping, 300 signifies first run and no sleep
result=300
# Ping til response
start=$(date +"%s")
while [ $result -ne 0 ]; do
now=$(date +"%s")
elapsed=$((now - start))
# Remaining time, could be infinite (no timeout)
if [ $timeout -ne 0 ]; then
remaining=$((timeout - elapsed))
else
if [ $(locale charmap) == "UTF-8" ]; then
remaining=∞
else
remaining=Inf
fi
fi
v Elapsed time: $elapsed - Remaining time: $remaining
# Timeout if need be
if [ $timeout -ne 0 ] && [ $remaining -lt 1 ]; then
echo "Timed out."
exit 1
fi
# Pause between pings, but not the first time through
if [ $result -ne 300 ]; then
sleep 1
fi
v Pinging $host
# Temporarily allow errors in commands
set +e
ping -c 1 $host 2> /dev/null 1>/dev/null
result=$?
set -e
v Result: $result
done