-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet2ips
executable file
·87 lines (73 loc) · 2.33 KB
/
net2ips
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
#! /usr/bin/env bash
set -u
set -e
# Convert an IPv4 network in CIDR notation to a list of IP addresses.
#
# Copyright (C) 2013-2022 Erik Auerswald <[email protected]>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
#
# Requires BASH and ipcalc (http://jodies.de/ipcalc) or my ipcalc replacement.
VERSION=2022-03-28-01
PROG="$(basename "$0")"
IPCALC="ipcalc"
IPCALCOPTS="-n -b"
version()
{
echo "$PROG version $VERSION"
echo "Copyright (C) 2013-2022 by Erik Auerswald <[email protected]>"
}
usage() { echo "Usage: $PROG [ { -h | -L | -V } | NETWORK ]"; }
help()
{
version
echo
usage
cat <<-EOH
Options:
-h print this help and exit
-L print license and exit
-V print version information and exit
NETWORK is a network description understood by ipcalc, e.g. 192.0.2.0/24.
Without arguments, network(s) are read from STDIN, one per line.
$PROG prints all possible host IP addresses of the network(s), one per line.
EOH
}
license()
{
cat <<-EOL
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
EOL
}
test $# -gt 2 && { usage; exit 1; }
while getopts hLV OPT; do
case "$OPT" in
h) help; exit 0;;
L) version; license; exit 0;;
V) version; exit 0;;
?) usage; exit 1;;
esac
done
shift $((OPTIND - 1))
if test $# -gt 0; then
# shellcheck disable=SC2086
RANGE=$("$IPCALC" $IPCALCOPTS "$@" | \
sed -n 's/^\(Network\|Broadcast\): *\([^ /]*\).*$/\2/p' | \
sed -n 'N;s/^\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)\n\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)/{\1..\5}.{\2..\6}.{\3..\7}.{\4..\8}/p')
eval printf -- '%s\\n' "$RANGE" | sed '1d;$d'
else
while read -r NET; do
# shellcheck disable=SC2086
RANGE=$("$IPCALC" $IPCALCOPTS $NET | \
sed -n 's/^\(Network\|Broadcast\): *\([^ /]*\).*$/\2/p' | \
sed -n 'N;s/^\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)\n\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)/{\1..\5}.{\2..\6}.{\3..\7}.{\4..\8}/p')
eval printf -- '%s\\n' "$RANGE" | sed '1d;$d'
done
fi
exit 0