-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpingselect
More file actions
executable file
·62 lines (50 loc) · 1.21 KB
/
pingselect
File metadata and controls
executable file
·62 lines (50 loc) · 1.21 KB
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
#!/bin/bash
TMPF=$(mktemp "/tmp/pingselect.XXXXXXX")
quit() {
[ -f "$TMPF" ] && rm -f "$TMPF"
echo -e "\nPingSelect ends!\n";
exit;
}
debug() {
return
echo "$@" 1>&2;
}
verbose_file() {
echo "$@" >> "${TMPF}";
}
verbose_progress() {
echo "$@";
}
trap quit SIGINT SIGTERM
if [ ! -f "$1" ];
then
echo "Specify path to file with server names"
exit
fi
echo "Pinging ..."
TOTAL=$(wc -l "$1"|awk '{ print $1 }')
CURR=0
verbose_progress -ne "\r${CURR}/${TOTAL} ... \r"
while read line;
do
IP=""
STATUS=""
CURR=$(($CURR + 1))
verbose_progress -ne "\r${CURR}/${TOTAL} ... please wait \r"
[ ! -z "${line}" ] && IP=$(nslookup "$line" | sed -n '/^Address/ s/Address: \([0-9\.]\+\)/\1/ p')
#debug "--- ping -q -n -c 1 -W 1 -i 0.5 \"${IP}\" (${line})| grep \"min/avg\""
[ ! -z "${IP}" ] && STATUS=$(ping -q -n -c 12 -W 1 -i 0.5 "$IP" | grep "min/avg")
#debug "--- $STATUS"
if [ -z "$STATUS" ];
then
verbose_file -n "Unreachable";
else
verbose_file -n $(echo -n "$STATUS" | sed 's/^.* [0-9\.]\+\/\([0-9\.]\+\)\/.*$/\1/');
fi
echo -n " ${line}" >> "${TMPF}";
verbose_file -n " (${IP})"
echo "" >> "${TMPF}";
done < "$1";
verbose_progress -e "\r... done "
cat $TMPF | sort -n;
quit