-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-ip.sh
143 lines (120 loc) · 3.92 KB
/
node-ip.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
#!/bin/bash
#
# Script for wireguard/batman gateways of Freifunk Muenchen to display the IP address of a connected node.
#
# node-ip.sh <fe80-address from map.ffmuc.net> <segment>
#
umask 022
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin"
usage() {
echo
echo "$0: Utility to display connecting node IP address on FFMuc batman/wireguard gateways"
echo
echo " $0 [options] [IPv6] [segment]"
echo
echo " Options:"
echo " -v | --verbose Verbose mode"
echo
echo " IPv6:"
echo " fe80 address of node from map.ffmuc.net"
echo
echo " segment: one of"
echo " muc_cty, muc_nord, muc_ost, muc_sued, muc_west, uml_nord,"
echo " uml_ost, uml_sued, uml_west, gauting, freising, welt"
echo
exit 1
}
GwURLs=("gw04.ext.ffmuc.net" \
"gw05.ext.ffmuc.net" \
"gw06.ext.ffmuc.net" \
"gw07.ext.ffmuc.net")
GwIPs=( "5.1.66.3" \
"5.1.66.4" \
"185.150.99.1" \
"185.150.99.2" \
"[2001:678:e68:ff00::3]" \
"[2001:678:e68:ff00::4]" \
"[2001:678:ed0:ff00::1]" \
"[2001:678:ed0:ff00::2]")
# check number of parameters
if [ $# -lt 2 -o $# -gt 3 ]; then
usage
fi
if [ $1 = "-v" -o $1 = "--verbose" ]; then
VERBOSE=true
shift
fi
NodeIPv6_1="$1"
# check if node ip starts with fe80
if [ "${NodeIPv6_1:0:5}" != "fe80:" ]; then
echo "Error: ipv6 $NodeIPv6_1 is not valid"
usage
fi
domain="$2"
[ $VERBOSE ] && echo domain = $domain
# check if domain/segment is known
shopt -s extglob
case $domain in !(muc_cty|muc_nord|muc_ost|muc_sued|muc_west|uml_nord|\
uml_ost|uml_sued|uml_west|gauting|freising|welt ))
echo "Error: segment $domain not known"
usage
esac
# one time ping so batman gets notified about a node address
ping6 -c1 $NodeIPv6_1%br-$domain >/dev/null 2>/dev/null
if [ $? -gt 0 ]; then
echo "Error: could not reach $NodeIPv6_1 through device br-$domain"
exit 1
fi
# get first MAC address
STRING1=$(batctl meshif bat-$domain ping -c1 $NodeIPv6_1%br-$domain | head -n1)
[ $VERBOSE ] && echo STRING1 = $STRING1
# at which position is the first open bracket
POS1=$(expr index "$STRING1" "\(")
[ $VERBOSE ] && echo POS1 = $POS1
# extract MAC address from answer
MAC1=${STRING1:$POS1:17}
[ $VERBOSE ] && echo MAC1 = $MAC1
# get second MAC address
STRING2=$(batctl meshif bat-$domain o | grep ${MAC1} | grep "^ \* ")
[ $VERBOSE ] && echo STRING2 = "$STRING2"
# extract MAC addrees from 6th word
MAC2=$(echo "$STRING2" | awk -v N=6 '{print $N}')
[ $VERBOSE ] && echo MAC2 = $MAC2
# lookup IP address
STRING3=$(bridge fdb show | grep $MAC2)
[ $VERBOSE ] && echo STRING3 = $STRING3
# get IP address from 5th word
NodeIPv6_2=$(echo "$STRING3" | awk -v N=5 '{print $N}')
[ $VERBOSE ] && echo NodeIPv6_2 = $NodeIPv6_2
# lookup node ip adress
if [ -d /sys/devices/virtual/net/wg-$domain ]; then
STRING4=$(wg show wg-$domain | grep -C3 $NodeIPv6_2 |grep endpoint)
[ $VERBOSE ] && echo STRING4 = $STRING4
elif [ -d /sys/devices/virtual/net/wg-uplink ]; then
STRING4=$(wg show wg-uplink | grep -C3 $NodeIPv6_2 |grep endpoint)
[ $VERBOSE ] && echo STRING4 = $STRING4
else
echo "wireguard interface not found"
exit 1
fi
# get node IP from 2nd word
STRING5=$(echo "$STRING4" | awk -v N=2 '{print $N}')
[ $VERBOSE ] && echo STRING5 = $STRING5
# cut port address from answer
POS2=${STRING5%:*}
[ $VERBOSE ] && echo POS2 = ${#POS2}
STRING6="${STRING5:0:${#POS2}}"
[ $VERBOSE ] && echo STRING6 = $STRING6
# filter out gateway ip addresses
for i in ${GwIPs[@]}; do
[ $VERBOSE ] && echo $i
if [ "$STRING6" = "$i" ]; then
echo
echo "Error: node not reachable from this gateway"
echo "Possibly try on other gateway"
exit 1
fi
done
# display node ip
echo Node IP = $STRING6
exit 0