Browse Source

Adds argument parser allowing to give a configuration file as argument

Yann Weber 3 years ago
parent
commit
118351ae6a
1 changed files with 47 additions and 14 deletions
  1. 47
    14
      ping_radar.sh

+ 47
- 14
ping_radar.sh View File

@@ -17,7 +17,6 @@
17 17
 alias echo='/bin/echo -e'
18 18
 
19 19
 REFRESH=2
20
-HOSTS_FILES="$HOME/.hosts_scanner"
21 20
 verbose=0
22 21
 res_file=$(mktemp -t hosts_scanner.XXXXXXXXXX)
23 22
 date_cmd="date --rfc-3339=s"
@@ -69,8 +68,6 @@ semrelease() {
69 68
 	rmdir $1
70 69
 }
71 70
 
72
-trap clean_exit 2
73
-
74 71
 upd_ping() {
75 72
 	# update ping stat file
76 73
 	# $1 host
@@ -168,6 +165,33 @@ cidr_scan() {
168 165
 	done
169 166
 }
170 167
 
168
+usage() {
169
+	echo "Usage : $0 [-c CONFIG_FILE] [-i HOST [-i HOST [...]]]" >&2
170
+}
171
+
172
+while getopts c:i: o
173
+do
174
+	case $o in
175
+	c)
176
+		HOSTS_FILES="$OPTARG";;
177
+	*)	usage; exit 1;;
178
+	esac
179
+done
180
+
181
+if [ -z "$HOSTS_FILES" ]
182
+then
183
+	HOSTS_FILES="$HOME/.hosts_scanner"
184
+fi
185
+
186
+if [ ! -f "$HOSTS_FILES" ]
187
+then
188
+	echo "File '$HOSTS_FILES' not found" >&4
189
+	usage
190
+	exit 2
191
+fi
192
+
193
+trap clean_exit 2
194
+
171 195
 # prepopulate file
172 196
 for h in $(cat "$HOSTS_FILES")
173 197
 do
@@ -193,24 +217,33 @@ do
193 217
 done
194 218
 sleep 1
195 219
 
220
+loop_count=0
196 221
 while [ 1 ]
197 222
 do
198 223
 	#sleep 1
199 224
 	#continue
200 225
 
201 226
 	semlock $lock
202
-	clear
203
-	echo "Ping radar $($date_cmd)\n"
204
-	printf "%30s %8s %30s\n" "host" "ping" "last up"
205
-	for i in $(seq 70); do echo -n "="; done
206
-	echo ""
207
-
208
-	#cat $res_file
209
-
210
-	#grep -v " down " $res_file
211
-	#grep " down " $res_file
227
+	#clear
228
+	loop_count=$(expr $loop_count + 1)
229
+	if [ $loop_count = "5" ]
230
+	then
231
+		clear;
232
+		loop_count=0
233
+	fi
234
+	echo -e "\x1B[0;0HPing radar $($date_cmd)\n$(printf "%30s %8s %30s\n" "host" "ping" "last up")\n$(for i in $(seq 70); do echo -n "="; done)\n$(sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/"  $res_file)"
212 235
 
213
-	sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/"  $res_file
236
+#	echo "Ping radar $($date_cmd)\n"
237
+#	printf "%30s %8s %30s\n" "host" "ping" "last up"
238
+#	for i in $(seq 70); do echo -n "="; done
239
+#	echo ""
240
+#
241
+#	#cat $res_file
242
+#
243
+#	#grep -v " down " $res_file
244
+#	#grep " down " $res_file
245
+#
246
+#	sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/"  $res_file
214 247
 
215 248
 	semrelease $lock
216 249
 	sleep $REFRESH

Loading…
Cancel
Save