-
Notifications
You must be signed in to change notification settings - Fork 0
/
tc.sh
executable file
·134 lines (123 loc) · 2.67 KB
/
tc.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright (C) 2020 <name of copyright holder>
# Author: Ágústsson, Kjartan Óli <[email protected]>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#! /usr/bin/bash
# Test a connection to a remote device using ping.
declare -r RELEASE_NUM="1"
declare -r FEATURE_NUM="4"
declare -r HOTFIX_NUM="6"
declare -r VERSION="$RELEASE_NUM.$FEATURE_NUM.$HOTFIX_NUM"
declare -r AUTHOR="Kjartan Óli Ágústsson"
verbosity=1
help()
{
cat <<- _EOF_
Usage: tc [OPTION] DESTINATION
Ping an IP address or Domain name DESTINATION once to test if it can be reached.
Example: tc 8.8.8.8
Options:
-V, --version Print version number and exit
-h, --help Display this help and exit
-s, --silent Supress all output (Usefull in scripts)
-v, --verbose Print the output of the ping attempt
_EOF_
}
version()
{
cat <<- _EOF_
tc (Test Connection) $VERSION
Written by $AUTHOR
_EOF_
}
acrgc=0
for option in $@
do
# Increment argc and assign to a garbage variable to avoid Command not found errors.
t=$((acrgc++))
case "$option" in
-v | --verbose)
if [[ $acrgc -eq $# ]]
then
help >&2
exit 1
fi
verbosity=2
;;
-s | --silent)
if [[ $acrgc -eq $# ]]
then
help >&2
exit 1
fi
verbosity=0
;;
-h | --help) help; exit 0;;
-V | --version)
version
exit 0;;
*)
# Check if this is the last option
if [[ $acrgc -eq $# ]]
then
# Assume the option is the DESTINATION and attempt to ping it.
break
else
# Unrecognized option, print help and exit with error.
help >&2
exit 1
fi
;;
esac
done
ip=$option
case "$verbosity" in
# Silent
0)
if ping -c 1 $ip > /dev/null 2> /dev/null
then
exit 0
else
exit 1
fi
;;
# Normal
1)
echo "Testing connection to $ip"
ping -c 1 $ip > /dev/null 2> /dev/null
res=$?
if [[ res -eq 0 ]]
then
echo "Test successfull"
exit 0
else
echo "Test failed"
exit 1
fi
;;
# Verbose
2)
echo "Testing connection to $ip"
ping -c 1 $ip
res=$?
if [[ res -eq 0 ]]
then
echo "Test successfull"
exit 0
else
echo "Test failed"
exit 1
fi
;;
esac