-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping-local-network.sh
executable file
·44 lines (37 loc) · 1.07 KB
/
ping-local-network.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
#!/usr/bin/env bash
ERR_NO_NET_TOOL="Error: No known-to-script-author tool found to get network
info. Can't ping unknown network."
ERR_NO_PREFIX="Error: Could not find address in 192.168.x.y namespace. Please check
'ip addr' or 'ifconfig' if you think you should be connected to such a
namespace. Otherwise please open a bug report with information on
if/why/how you think it would be appropriate to support your network's
namespace and/or tools."
data=
if which ip >/dev/null; then
data="$(ip addr)"
elif which ifconfig >/dev/null; then
data="$(ifconfig)"
else
echo "$ERR_NO_NET_TOOL"
exit 1
fi
prefix=
for i in {0..255}; do
pre="192.168.$i."
if echo "$data" | grep -q "$pre"; then
prefix="$pre"
break
fi
done
if [ -z "$prefix" ]; then
echo "$ERR_NO_PREFIX"
exit 1
fi
doit() {
# Ping $1 with count=1, wait=5s, and only show results if successful.
ping -c 1 -W 5s "$1" | grep -B 1 "1 received" | grep -o "$1"
}
for ((x=1; x<255; x++));
do
doit "$prefix$x" &
done | sort --version-sort # This sort adds an implicit "wait".