-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnetter.sh
executable file
·74 lines (68 loc) · 1.91 KB
/
subnetter.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
#!/bin/bash
IP=`echo $1 | cut -d / -f1`
ip1=$(echo ${IP} | tr "." " " | awk '{ print $1 }')
ip2=$(echo ${IP} | tr "." " " | awk '{ print $2 }')
ip3=$(echo ${IP} | tr "." " " | awk '{ print $3 }')
ip4=$(echo ${IP} | tr "." " " | awk '{ print $4 }')
r=$2
d=$2
sub=`echo $1 | cut -d / -f2`
b=0
if [[ "$sub" -ge "24" ]]
then
x=4
m=$((32-sub))
elif [[ "$sub" -ge "16" && "$sub" -le "23" ]]
then
x=3
m=$((24-sub))
elif [[ "$sub" -ge "8" && "$sub" -le "15" ]]
then
x=2
m=$((16-sub))
elif [[ "$sub" -ge "0" && "$sub" -le "7" ]]
then
x=1
m=$((8-sub))
fi
c=$((2**$m))
li=$((ip$x+c-1))
for r in $(eval echo {$r..1} )
do
n=$m
q=$(($c/$r))
while [[ "$q" -le "$((2**$n))" && "$n" -gt "0" ]]
do
if [[ "$q" -eq "$((2**$n))" || "$q" -gt "$((2**($n-1)))" && "$n" -gt "0" ]]
then
p=$((2**$n))
c=$((c-p))
sn=$((li-c+1))
sb=$((8*$x-n))
for j in $(eval echo {$n..1} )
do
s=$((2**($j-1)))
b=$((b+s))
done
fi
(( n-- ))
done
d=$d-$r
if [[ "$sub" -ge "24" ]]
then
v=$ip4
echo "subnet=$ip1.$ip2.$ip3.$((sn-p))/$sb" "network=$ip1.$ip2.$ip3.$((sn-p))" "broadcast=$ip1.$ip2.$ip3.$((sn-1))" "gateway=$ip1.$ip2.$ip3.$((sn-p+1))" "hosts=$(($p-3))"
elif [[ "$sub" -ge "16" && "$sub" -le "23" ]]
then
v=$ip3
echo "subnet=$ip1.$ip2.$((sn-p)).$ip4/$sb" "network=$ip1.$ip2.$((sn-p)).$ip4" "broadcast=$ip1.$ip2.$((sn-1)).255" "gateway=$ip1.$ip2.$((sn-p+1)).$ip4" "hosts=$(($p-3))"
elif [[ "$sub" -ge "8" && "$sub" -le "15" ]]
then
v=$ip2
echo "subnet=$ip1.$((sn-p)).$ip3.$ip4/$sb" "network=$ip1.$((sn-p)).$ip3.$ip4" "broadcast=$ip1.$((sn-1)).255.255" "gateway=$ip1.$((sn-p+1)).$ip3.$ip4" "hosts=$(($p-3))"
elif [[ "$sub" -ge "0" && "$sub" -le "7" ]]
then
v=$ip1
echo "subnet=$((sn-p)).$ip2.$ip3.$ip4/$sb" "network=$((sn-p)).$ip2.$ip3.$ip4" "broadcast=$((sn-1)).255.255.255" "gateway=$((sn-p+1)).$ip2.$ip3.$ip4" "hosts=$(($p-3))"
fi
done