-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell-ip-tools.sh
More file actions
361 lines (292 loc) · 9.28 KB
/
Copy pathshell-ip-tools.sh
File metadata and controls
361 lines (292 loc) · 9.28 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/sh
# shellcheck disable=SC2154,SC2086,SC2004
# shell-ip-tools.sh
# Copyright: antonk (antonk.d3v@gmail.com)
# github.com/friendly-bits
# Library for ip addresses processing, detecting LAN ipv4 and ipv6 subnets and for subnets aggregation
# Only use get_lan_subnets() and detect_lan_subnets() on a machine which has no dedicated WAN interfaces (physical or logical)
# otherwise WAN subnet may be wrongly detected as LAN subnet
# aggregate_subnets() requires the $_nl variable to be set
# detect_lan_subnets() and get_lan_subnets() require variables $subnet_regex_[ipv4|ipv6] to be set
# Usage for lan subnets detection (no aggregation):
# call detect_lan_subnets (requires family as 1st argument - ipv4|inet|ipv6|inet6)
# Usage for lan subnets detection + aggregation:
# call get_lan_subnets (requires family as 1st argument - ipv4|inet|ipv6|inet6)
# Usage for subnets/ip's aggregation:
# pipe input subnets (newline-separated) into aggregate_subnets (requires family as 1st argument - ipv4|inet|ipv6|inet6)
## Functions
# Trims input ipv4 or ipv6 address to mask bits and outputs the result represented as integer
# 1 - ipv4 address (delimited with '.') or ipv6 address (delimited with ':')
# 2 - family (ipv4|inet|ipv6|inet6)
# 3 - mask bits (integer)
# 4 - var name for output
ip_to_int() {
ip_itoint="$1"
family_itoint="$2"
ip2int_maskbits="$3"
out_var_itoint="$4"
# ipv4
case "$family_itoint" in ipv4|inet)
# number of bits to trim
bits_trim=$((32-ip2int_maskbits))
# convert ip to int and trim to mask bits
IFS_OLD_itoint="$IFS"
IFS='.'
set -- $ip_itoint
IFS=" "
for octet in "$1" "$2" "$3" "$4"; do
case "$octet" in
[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ;;
*)
printf '%s\n' "ip_to_int: invalid octet '$octet'" >&2
IFS="$IFS_OLD_itoint"
return 1
esac
done
IFS="$IFS_OLD_itoint"
ip2int_conv_exp="(($1<<24) + ($2<<16) + ($3<<8) + $4)>>$bits_trim<<$bits_trim"
eval "$out_var_itoint=$(( $ip2int_conv_exp ))"
return 0
esac
# ipv6
# expand ipv6 and convert to int
# process enough chunks to cover $maskbits bits
IFS_OLD_itoint="$IFS"
IFS=':'
set -- $ip_itoint
IFS="$IFS_OLD_itoint"
bits_processed=0
chunks_done=0
missing_chunks=$((8-$#))
ip_itoint=
for chunk in "$@"; do
# print 0's in place of missing chunks
case "${chunk}" in '')
missing_chunks=$((missing_chunks+1))
while :; do
case $missing_chunks in 0) break; esac
bits_processed=$(( bits_processed + 16 ))
chunks_done=$((chunks_done+1))
ip_itoint="${ip_itoint}0 "
case $(( ip2int_maskbits - bits_processed )) in 0|-*) break 2; esac
missing_chunks=$((missing_chunks-1))
done
continue ;;
esac
case "$chunk" in
????|???|??|?) ;;
*)
printf '%s\n' "ip_to_int: invalid chunk '$chunk'" >&2
return 1
esac
case "$chunk" in
*[!a-f0-9]*)
printf '%s\n' "ip_to_int: invalid chunk '$chunk'" >&2
return 1
esac
bits_processed=$(( bits_processed + 16 ))
chunks_done=$((chunks_done+1))
case $(( ip2int_maskbits - bits_processed )) in
0)
ip_itoint="${ip_itoint}$(( 0x${chunk} )) "
break ;;
-*)
bits_trim=$(( bits_processed - ip2int_maskbits ))
ip_itoint="${ip_itoint}$(( 0x${chunk}>>bits_trim<<bits_trim )) "
break ;;
*)
ip_itoint="${ip_itoint}$(( 0x${chunk} )) "
esac
done
# replace remaining chunks with 0's
while :; do
case $(( 8 - chunks_done )) in 0) break; esac
ip_itoint="${ip_itoint}0 "
chunks_done=$(( chunks_done + 1 ))
done
eval "$out_var_itoint=\"$ip_itoint\""
:
}
# Converts input integer(s) into ip address with optional mask bits
# For ipv6, input should be 8 whitespace-separated integer numbers
# 1 - input ip address represented as integer(s)
# 2 - family (ipv4|inet|ipv6|inet6)
# 3 - optional: mask bits (integer). if specified, appends /[maskbits] to output
int_to_ip() {
case "$3" in
'') maskbits_iti='' ;;
*) maskbits_iti="/$3"
esac
case "$2" in
ipv4|inet)
printf '%s\n' "$(( ($1>>24)&255 )).$(( ($1>>16)&255 )).$(( ($1>>8)&255 )).$(($1 & 255))${maskbits_iti}" ;;
ipv6|inet6)
# convert into 16-bit hex chunks delimited with ':'
set -- $1
printf ':%x' $* |
# convert to ipv6 and compress
{
hex_to_ipv6 || exit 1
printf '%s\n' "${maskbits_iti}"
}
esac
}
# input via STDIN: 16-bit hex chunks with ':' preceding each
# output via STDOUT (without newline)
hex_to_ipv6() {
IFS='' read -r ip_hti
# compress 0's across neighbor chunks
IFS=' '
for zeroes in ":0:0:0:0:0:0:0:0" ":0:0:0:0:0:0:0" ":0:0:0:0:0:0" ":0:0:0:0:0" ":0:0:0:0" ":0:0:0" ":0:0"; do
case "$ip_hti" in *$zeroes*)
ip_hti="${ip_hti%%"$zeroes"*}::${ip_hti#*"$zeroes"}"
break
esac
done
# replace ::: with ::
case "$ip_hti" in *:::*) ip_hti="${ip_hti%%:::*}::${ip_hti#*:::}"; esac
# trim leading colon if it's not a double colon
case "$ip_hti" in
:[!:]*) ip_hti="${ip_hti#:}"
esac
printf %s "${ip_hti}"
}
# trims input subnets to maskbits, removes ip's and subnets which are encapsulated in other subnets
# input via STDIN: newline-separated subnets and/or ip's
# output via STDOUT: newline-separated subnets
# 1 - family (ipv4|ipv6|inet|inet6)
aggregate_subnets() {
inv_maskbits() {
printf '%s\n' "aggregate_subnets: invalid mask bits '$1'" >&2
}
family_ags="$1"
case "$1" in
ipv4|inet) ip_len_bits=32 ;;
ipv6|inet6) ip_len_bits=128 ;;
*) printf '%s\n' "aggregate_subnets: invalid family '$1'." >&2; return 1
esac
processed_maskbits=' '
nonempty_ags=
# print with maskbits prepended
awk -F/ "\$0{ if (\$2) {mb=\$2} else {mb=\"$ip_len_bits\"}; print mb \"/\" \$1}" |
# sort by mask bits, add magic string in final line
{
sort -n
printf '%s\n' EOF_AGS
} |
# process subnets
while IFS="$_nl" read -r subnet1; do
case "$subnet1" in
'') continue ;;
EOF_AGS)
[ "$nonempty_ags" ] && exit 254 # 254 means OK here
exit 1
esac
# get mask bits
maskbits="${subnet1%/*}"
case "$maskbits" in *[!0-9]*)
inv_maskbits "$maskbits"
exit 1
esac
case "$maskbits" in ?|??|???) ;; *)
inv_maskbits "$maskbits"
exit 1
esac
case $((ip_len_bits-maskbits)) in -*)
inv_maskbits "$maskbits"
exit 1
esac
# chop off mask bits
ip1_ags="${subnet1#*/}"
# convert ip to int and trim to mask bits
ip_to_int "$ip1_ags" "$family_ags" "$maskbits" ip1_int || exit 1
# don't print if trimmed IP int is included in ${res_ips_by_mb_${maskbits}}
IFS=' '
bits_processed=0
ip1_trim=
set -- $ip1_int
chunk=
for mb in $processed_maskbits; do
chunks_done_last=0
eval "mb_ips=\"\${res_ips_by_mb_${mb}}\""
case "$family_ags" in
ipv4|inet)
bits_trim=$((32-mb))
ip1_trim=$(( ip1_int>>bits_trim<<bits_trim )) ;;
ipv6|inet6)
# process $mb bits
for chunk in "$@"; do
case $(( mb - (bits_processed+16) )) in
0)
bits_processed=$(( bits_processed + 16 ))
chunks_done_last=$(( chunks_done_last + 1 ))
ip1_trim="${ip1_trim}${chunk}"
chunk=
break ;;
-*)
bits_trim=$(( bits_processed + 16 - mb ))
chunk=$(( chunk>>bits_trim<<bits_trim ))
break ;;
*)
bits_processed=$(( bits_processed + 16 ))
chunks_done_last=$(( chunks_done_last + 1 ))
ip1_trim="${ip1_trim}${chunk} "
esac
done
esac
shift $chunks_done_last
case "$mb_ips" in *"${_nl}${ip1_trim}${chunk} "*) continue 2; esac
done
# add current IP to ${res_ips_by_mb_${maskbits}
eval "res_ips_by_mb_${maskbits}=\"\${res_ips_by_mb_${maskbits}}${_nl}${ip1_int} \""
# add current ip to $res_ips_int
res_ips_int="${res_ips_int}${ip1_int} ${_nl}"
# add current maskbits to $processed_maskbits
case "$processed_maskbits" in *" $maskbits "*) ;; *)
processed_maskbits="${processed_maskbits}${maskbits} "
esac
# convert back to ip and print out
int_to_ip "$ip1_int" "$family_ags" "$maskbits" || {
printf '%s\n' "Failed to convert '$ip1_int' to ip." >&2
exit 1
}
nonempty_ags=1
done
case $? in
254) return 0 ;;
*) cat >/dev/null; return 1
esac
}
# Outputs newline-separated subnets
# 1 - family (ipv4|inet|ipv6|inet6)
detect_lan_subnets() {
case "$1" in
ipv4|inet)
case "$subnet_regex_ipv4" in '') printf '%s\n' "detect_lan_subnets: regex is not set" >&2; return 1; esac
ifaces="dummy_123|$(
ip -f inet route show table local scope link |
sed -n '/[ ]lo[ ]/d;/[ ]dev[ ]/{s/.*[ ]dev[ ][ ]*//;s/[ ].*//;p}' | tr '\n' '|')"
ip -o -f inet addr show | grep -E "${ifaces%|}" | grep -oE "$subnet_regex_ipv4" ;;
ipv6|inet6)
case "$subnet_regex_ipv6" in '') printf '%s\n' "detect_lan_subnets: regex is not set" >&2; return 1; esac
ip -o -f inet6 addr show |
grep -oE 'inet6[ ]+(fd[0-9a-f]{0,2}:|fe80:)[0-9a-f:/]+' | grep -oE "$subnet_regex_ipv6\$" ;;
*) printf '%s\n' "detect_lan_subnets: invalid family '$1'." >&2; return 1
esac
}
# 1 - family (ipv4|ipv6|inet|inet6)
get_lan_subnets() {
detect_lan_subnets "$1" |
aggregate_subnets "$1" ||
{ printf '%s\n' "Failed to detect $1 LAN subnets." >&2; return 1; }
}
# Required constants
_nl='
'
ipv4_regex='((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])\.){3}(25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])'
maskbits_regex_ipv4='(3[0-2]|([1-2][0-9])|[6-9])'
subnet_regex_ipv4="${ipv4_regex}/${maskbits_regex_ipv4}"
ipv6_regex='([0-9a-f]{0,4})(:[0-9a-f]{0,4}){2,7}'
maskbits_regex_ipv6='(12[0-8]|((1[0-1]|[1-9])[0-9])|[6-9])'
subnet_regex_ipv6="${ipv6_regex}/${maskbits_regex_ipv6}"
: