This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examenScript.sh
executable file
·273 lines (238 loc) · 5.93 KB
/
examenScript.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
# Default subnet
SUBNET=129 #129
# Changes the timeout (in seconds) of the pingFunction
TIME_OUT=1
# the default network address
NETWORK_ADDRESS="192.168."
# Prints the help in a man-page format
function help() {
clear
echo "NAME"
echo " Pinger - A tool used to ping a range of IP's."
echo ""
echo "SYNOPSIS"
echo " $0 [OPTION] [IP_1] [IP_2] ... [IP_N]"
echo ""
echo "DESCRIPTION"
echo " Pinger was made to verify the status of 1 or more IP addresses in"
echo " a local area network."
echo " e.g: $0 87 97 132"
echo ""
echo "OPTIONS"
echo " -h, -help"
echo " help will show the user basic information about the script"
echo ""
echo " -t"
echo " this adds 200 to each number, e.g: -t 17 will test IP 217"
echo ""
echo " --up"
echo " shows you all the IP's that are up and running"
echo ""
echo " --sum"
echo " shows you a summary table of all the failed and successful pings"
echo ""
echo " --sort"
echo " pings unique IP's in descending order"
echo ""
echo " -sn [subnet], -sn[subnet]"
echo ' "-sn 128" will test the subnetwork: 128 and not 192.168."129".0/24 '
echo ""
echo " -mac"
echo " every successful ping will receive the corresponding MAC-address"
echo ""
echo "AUTHOR"
echo " Witten by Verscheure Bengt and Miers Maarten."
exit 1
}
# Ping-function
# will ping given ip and echo
# the ping result.
# in case the ping was successful
# then it will return true, otherwise
# it will return false
function pingFunction() {
# -w 5 = our default timeout is 5, but this can be changed
# -c 1 = only send 1 ping packet
ping -w $TIME_OUT -c 1 $NETWORK_ADDRESS$SUBNET.$1 &> /dev/null
# the exit-code of the ping-command will be stored inside '$?'
# in case it's 0, the ping was successful.
# for more info see: http://www.manpagez.com/man/8/ping/
if [[ $? -eq 0 ]]
then
return 0
else
return 1
fi
}
# This function is used to verify whether or not the given
# parameter is a valid byte
function isByte() {
# check if the parameter is a byte
# use regex to check if the input is a number
# ^ = beginning of the string
# [0-9] = character set, matching input from 0 to 9
# + = one or more
# $ = end of the string, else 11a would match too
# (thanks regexr.com)
# then just use integer comparison operators to check if its within the
# bound of a byte
if [[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -le "255" ] &> /dev/null && [ "$1" -ge "0" ] &> /dev/null
then
return 0
else
return 1
fi
}
# This function will add the
# given host address to the list, only if it ranges between {0..255}
function addHostToList() {
if isByte $1
then
HOST_LIST="$HOST_LIST $1"
else
echo "Skipping '$1' because it's an invalid host-address! (Should be between 0 & 255)"
fi
}
# This function adds the given
# range of host-addresses to the HOST_LIST
function addHostRangeToList() {
[[ $1 =~ ([0-9]+)-([0-9]+) ]] # Use regex to split the input
left=${BASH_REMATCH[1]} # get the first number
right=${BASH_REMATCH[2]} # get the second number
if [ $2 ]
then
left=$(( $left + 200 ))
right=$(( $right + 200 ))
fi
for (( i=($left); i <= ($right); i++ ))
do
addHostToList $i
done
}
# Sets the subnet to the given parameter
function setSubnet() {
if isByte $1
then
SUBNET=$1
else
echo "'$1' is an invalid subnet!"
exit
fi
}
# Returns the mac address of the given ip (in case it can be found)
function getMacAddress() {
# source: http://forums.fedoraforum.org/showpost.php?p=1496180&postcount=2
echo "$(arp -an "$NETWORK_ADDRESS$SUBNET.$1" | grep "$1" | awk '{print $4}')"
}
# Puts 'up', 'sum', 'sorting' and 'mac' on the default: false
up=false
sum=false
sorting=false
mac=false
if [ -z "$1" ]
then
echo "You did not enter a parameter. Please use '$0 -h' to show the usage."
exit 1
else
until [ -z $1 ]
do
case $1 in
[a-z]* )
echo "Invalid character! Please use the helpfunction: -help"
;;
-h | -help )
help
;;
--up )
up=true
;;
--sum )
sum=true
;;
--sort )
sorting=true
;;
*[0-9]-[0-9]* )
addHostRangeToList $1
;;
-t )
shift
if isByte $1
then
addHostToList $(( $1 + 200 ))
else
addHostRangeToList $1 true
fi
;;
-sn )
shift
setSubnet $1
;;
-sn[0-9]* )
snet=`echo "$1" | awk -F"n" '{ print $2 }'`
setSubnet $snet
;;
-mac )
mac=true
;;
[0-9]* )
addHostToList $1
;;
esac
shift
done
fi
# Start printing our stuff
# the --sort flag was set so sort the list before pinging
if [ $sorting = true ]
then
# sort the host list
# 'sort' only works with lines, hence why we convert our array to lines
# n = numeric
# u = unique (no dupes)
HOST_LIST=$(echo ${HOST_LIST[@]} | tr ' ' '\n' | sort -nu | tr '\n' ' ')
fi
# loop through the HOST_LIST and ping each entry. Then put the host in the correct list
# depending on the ping result
for host in ${HOST_LIST[@]}
do
if pingFunction $host
then
UP_LIST="$UP_LIST $host"
echo -n "$host is up."
if [ $mac = true ]
then
echo " --- mac: $(getMacAddress $host)"
else
echo
fi
else
DOWN_LIST="$DOWN_LIST $host"
if [ $up = false ]
then
echo "$host is down."
fi
fi
done
# Count the lists using wc
UP_COUNT=$(echo ${UP_LIST[@]} | wc -w)
DOWN_COUNT=$(echo ${DOWN_LIST[@]} | wc -w)
TOTAL_COUNT=$(echo ${HOST_LIST[@]} | wc -w)
# the --sum flag was enabled so print a summary of our UP & DOWN lists
if [ $sum = true ]
then
# Print the UP_LIST first, in case it's not empty
if [ $UP_COUNT -gt 0 ]
then
echo "Up = $(echo ${UP_LIST[@]} | tr ' ' ', ')"
fi
# Print the DOWN_LIST, again in case it's not empty
if [ $DOWN_COUNT -gt 0 ]
then
echo "Down = $(echo ${DOWN_LIST[@]} | tr ' ' ', ')"
fi
fi
# Print the stats
echo "$UP_COUNT/$TOTAL_COUNT of the pinged computers are up"
echo "That is $(( $UP_COUNT * 100 / $TOTAL_COUNT ))% of all the computers"