-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsnext
More file actions
executable file
·352 lines (310 loc) · 6.82 KB
/
nsnext
File metadata and controls
executable file
·352 lines (310 loc) · 6.82 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
#!/bin/sh
############################################################ IDENT(1)
#
# $Title: Script to get next free IP address from nsadmin zone $
# $Copyright: 2019 Devin Teske. All rights reserved. $
# $FrauBSD: nsadmin/nsnext 2019-10-31 09:15:23 -0700 freebsdfrau $
#
############################################################ CONFIGURATION
NSNEXT_CONF=nsnext.conf # See OS Glue
############################################################ GLOBALS
VERSION='$Version: 1.0.5 $'
pgm="${0##*/}" # Program basename
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
#
# Global exit status
#
SUCCESS=0
FAILURE=1
#
# OS Glue
#
: ${UNAME_s:=$( uname -s )}
#
# Command-line options
#
COUNT= # -N num
LOCAL= # -l
PING=1 # -n | -p
SHOW_ALL= # -a
#
# Miscellaneous
#
BLACKLIST=
CONSOLE=1
[ -t 1 ] || CONSOLE=
SERVER=
ZONE=
############################################################ FUNCTIONS
die()
{
local fmt="$1"
if [ $# -gt 0 ]; then
shift 1 # fmt
printf "%s: $fmt\n" "$pgm" "$@" >&2
fi
exit $FAILURE
}
usage()
{
local optfmt="\t%-5s %s\n"
exec >&2
if [ $# -gt 0 ]; then
local fmt="$1"
shift 1 # fmt
printf "%s: $fmt\n" "$pgm" "$@"
fi
printf "Usage: %s [-ahlnpv] [-N num] %s [nameserver]\n" \
"$pgm" "alias|cidr|network|arpa"
printf "Options:\n"
printf "$optfmt" "-a" "Show all available addresses (implies \`-n')."
printf "$optfmt" "-h" "Print usage statement and exit."
printf "$optfmt" "-l" "Query local name server."
printf "$optfmt" "-N num" "Print num addresses."
printf "$optfmt" "-n" "Do not attempt to ping addresses."
printf "$optfmt" "-p" "Always attempt ping, even if given \`-a'."
printf "$optfmt" "-v" "Print version and exit."
die
}
############################################################ MAIN
#
# Process command-line options
#
while getopts ahlN:npv flag; do
case "$flag" in
a) SHOW_ALL=1 PING= ;;
l) LOCAL=1 ;;
N) COUNT="$OPTARG" ;;
n) PING= ;;
p) PING=1 ;;
v) VERSION="${VERSION#*: }"
echo "${VERSION% $}"
exit $SUCCESS ;;
*) usage # NOTREACHED
esac
done
shift $(( $OPTIND - 1 ))
#
# Validate command-line options
#
case "$COUNT" in
*[!0-9]*) usage "\`-N num' requires numerical argument" ;; # NOTREACHED
esac
#
# Validate command-line arguments
#
if [ $# -eq 0 ]; then
usage "Not enough arguments"
# NOTREACHED
elif [ $# -gt 2 ]; then
usage "Too many arguments"
# NOTREACHED
elif [ $# -eq 2 -a "$LOCAL" ]; then
usage "Too many arguments (cannot use \`-l' with multiple arguments)"
# NOTREACHED
fi
#
# Command-line arguments
#
ZONE=$( echo "$1" | awk '{print tolower($0)}' )
if [ "$LOCAL" ]; then
SERVER=127.0.0.1
elif [ $# -eq 2 ]; then
SERVER="$2"
fi
#
# OS Glue
#
case "$UNAME_s" in
*BSD*) NSNEXT_CONF="/usr/local/etc/$NSNEXT_CONF" ;;
*) NSNEXT_CONF="/etc/$NSNEXT_CONF"
esac
#
# Load config
#
. "$NSNEXT_CONF" || die
#
# Check configured aliases and convert to in-addr.arpa
#
found_alias=
for alias in $aliases; do
[ "$ZONE" = "$alias" ] || continue
found_alias=1
break
done
if [ "$found_alias" ]; then
eval BLACKLIST=\"\$${ZONE}_blacklist\"
eval ZONE=\"\$$ZONE\" # Replace with defined alias
else
case "$ZONE" in
*.in-addr.arpa|*.in-addr.arpa.)
ZONE=$( echo "$ZONE" | awk '{
o = ""
sub(/\.in-addr\.arpa\.?/, "")
p = split($0, q, /\./)
for (r = p; r > 0; r--) o = o "." q[r]
for (r = p; r < 4; r++) o = o ".0"
print substr(o, 2)
}' )
;;
esac
case "$ZONE" in
*/*) : good ;;
*[!0-9.]*) : name ;;
*) ZONE="$ZONE/24"
esac
fi
#
# Find next available IP address
#
trap echo SIGINT
export BLACKLIST
export COUNT
export PING
export SHOW_ALL
echo $ZONE | xargs -n1 | awk -v server="$SERVER" -v console=$CONSOLE '
################################################## BEGIN
BEGIN {
stderr = "/dev/stderr"
delete blacklist # Pedantic
n = split(ENVIRON["BLACKLIST"], list, /[[:space:]]+/)
for (i = 1; i <= n; i++) blacklist[list[i]]
count = ENVIRON["COUNT"]
do_count = ENVIRON["COUNT"] != ""
ping = ENVIRON["PING"]
show_all = ENVIRON["SHOW_ALL"]
}
################################################## FUNCTIONS
function atoi(ip, n, o, p, q, r)
{
n = o = 0
delete q # Pedantic
p = split(ip, q, /\./)
for (r = p; r > 0; r--) o += lshift(q[r], 8 * n++)
return o
}
function mask(bits, n, o)
{
o = 0
while (bits > 0) o += lshift(1, 32 - bits--)
return o
}
function itoa(inet, o, p)
{
o = ""
for (p = 3; p >= 0;)
o = o "." and(rshift(inet, 8 * p--), 255)
return substr(o, 2)
}
function itor(inet, o, p)
{
o = "in-addr.arpa."
for (p = 3; p > 0;)
o = and(rshift(inet, 8 * p--), 255) "." o
return o
}
function rtoa(arpa, o, p, q)
{
o = ""
delete p # Pedantic
q = split(arpa, p, /\./)
return sprintf("%u.%u.%u.%u", p[4], p[3], p[2], p[1])
}
function get_used(subnet, arpa, cmd, rec, words, ip, nrecs)
{
arpa = itor(subnet)
delete used # Clear global
cmd = sprintf("host -l %s %s 2> /dev/null && echo success",
arpa, server)
nrecs = 0
while (cmd | getline rec > 0) {
if (rec !~ / domain name pointer /) continue
nrecs++
delete words # Pedantic
split(rec, words, /[[:space:]]+/)
ip = rtoa(words[1])
used[ip]
}
close(cmd)
if (rec != "success") {
warn(sprintf("Unable to transfer zone: %s", arpa))
return 0
}
if (nrecs == 0) {
warn(sprintf("Empty zone: %s", $0))
return 0
}
return 1
}
function warn(msg)
{
if (console)
printf "\033[33;1mWARNING!\033[m %s\n", msg > stderr
else
printf "WARNING! %s\n", msg > stderr
fflush(stderr)
}
function emit(str)
{
printf "%s", str
fflush()
}
function erase()
{
if (!console) return
emit("\r\033[K")
}
function puts(str)
{
emit(str "\n")
}
function is_available(ip, cmd, buf)
{
if (ping) {
if (console) emit(sprintf("\033[2mPinging %s ... \033[m", ip))
cmd = sprintf("ping -c1 %s && echo success", ip)
while (cmd | getline buf > 0) { }
erase()
if (buf == "success") {
warn(sprintf("%s in use but not in DNS", ip))
return 0
}
}
puts(ip)
return 1
}
################################################## MAIN
{
if (docount && count <= 0) exit
if (!match($0, "/[0-9]+$")) {
warn(sprintf("Invalid argument: %s", $0))
next
}
bits = substr($0, RSTART + 1)
netb = substr($0, 1, RSTART - 1)
inet = atoi(netb)
imask = mask(bits)
rmask = and(compl(imask), 4294967295)
network = and(inet, imask)
broadcast = or(inet, rmask)
for (subnet = network; subnet < broadcast; subnet += 256) {
if (!get_used(subnet)) continue # Get already-used addresses
for (inum = subnet; inum < subnet + 256; inum++) {
if (inum == network || inum == broadcast) continue
ip = itoa(inum)
if (ip in blacklist) continue
if (ip in used) continue
if (is_available(ip)) {
if (do_count) {
if (--count <= 0) exit
} else if (!show_all)
exit
}
}
}
}
' # END-QUOTE
exit $SUCCESS
################################################################################
# END
################################################################################