-
Notifications
You must be signed in to change notification settings - Fork 2
/
findroute
executable file
·73 lines (64 loc) · 1.39 KB
/
findroute
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
#!/bin/bash
# (c) 2020 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/findroute
if [ -z "$(command -v seq)" ]
then
seq() {
local start=${1:-2}
local step=${2:-1}
local end=${3:-254}
if [ "$#" -eq 2 ]; then
end=$step
step=1
fi
if [ "$step" -gt 0 ]; then
while [ "$start" -le "$end" ]; do
echo "$start"
start=$((start + step))
done
else
while [ "$start" -ge "$end" ]; do
echo "$start"
start=$((start + step))
done
fi
}
fi
links=$(ip link show | grep '^[0-9]' | grep -vw lo: | wc -l)
if [ ${links:-999} -lt 2 ]
then
echo "all routes: $(ip route show default)"
exit 0
fi
IP=${1//[^0-9a-fA-F.:]/}
if [ "$1" != "${IP}" ]
then
echo "invalid IPv4 or IPv6 address"
exit 1
fi
if [ -z "${IP}" ]
then
echo "IPv4/IPv6 address to find route for?"
exit 1
fi
if [ "${IP##*:*}" != "${IP}" ]
then
six="-6"
tcidr=128
else
six=""
tcidr=32
fi
for cidr in $(seq $tcidr -1 0)
do
status=$(ip ${six} route show ${IP}/${cidr})
if [ -n "${status}" ]
then
status=${status// via/, routing via}
echo "${IP} contained in ${status}"
exit 0
fi
done
echo "unable to determine route. that's odd"