-
Notifications
You must be signed in to change notification settings - Fork 4
/
add-new-ranges.sh
166 lines (146 loc) · 4.38 KB
/
add-new-ranges.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
#
# Simple way to add IPs ranges to CentOS/RHEL system
# (c) Jonas Bunevicius, 2018
#
################## chk_os_version() #####################
chk_os_version() {
if [ $1 -eq 0 ]
then
echo
echo "Sorry, script supports only CentOS/RHEL based systems..."
echo
exit 1
fi
}
#########################################################
################# chk_subnet_input() ####################
chk_subnet_input() {
mask=$(echo $1 | awk -F '/' '{print $2}')
if [[ $mask =~ ^[0-9]+$ ]]
then
if [ $mask -lt 16 ]
then
echo "Range $1 not supported yet..."
exit 1
elif [ $mask -gt 31 ]
then
echo "Range $1 is invalid..."
exit 1
fi
else
echo "Range $1 was defined incorrectly..."
exit 1
fi
}
#########################################################
################# upd_clonenum_start() ##################
upd_clonenum_start() {
if [ -z $clonenum_start ]
then
clonenum_start=$(ifconfig | grep ^eth0: | tail -n1 |\
awk -F ' ' '{print $1}' | cut -d ':' -f2)
already_checked=0
fi
if [ $already_checked -eq 0 ]
then
if [ -z $clonenum_start ]
then
clonenum_start=0
else
let clonenum_start++
fi
already_checked=1
else
let clonenum_start+=$prv_step
fi
prv_step=$step
}
##########################################################
################## add_ifcfg-range() #####################
add_ifcfg-range() {
if [ -z $num ]
then
num=$(ls -l /etc/sysconfig/network-scripts/ | grep ifcfg-eth0-.* |\
awk -F ' ' '{print $9}' | sed 's/.*range//' | sort -n | tail -n1)
fi
if [ -z $num ]
then
num=0
cd /etc/sysconfig/network-scripts && touch ifcfg-eth0-range${num}
else
let num++
cd /etc/sysconfig/network-scripts && touch ifcfg-eth0-range${num}
fi
echo "IPADDR_START="${ipaddr_start} >> /etc/sysconfig/network-scripts/ifcfg-eth0-range${num}
echo "IPADDR_END="${ipaddr_end} >> /etc/sysconfig/network-scripts/ifcfg-eth0-range${num}
echo "NETMASK="${netmask} >> /etc/sysconfig/network-scripts/ifcfg-eth0-range${num}
echo "CLONENUM_START="${clonenum_start} >> /etc/sysconfig/network-scripts/ifcfg-eth0-range${num}
echo ARPCHECK=no >> /etc/sysconfig/network-scripts/ifcfg-eth0-range${num}
}
##########################################################
#################### calc_boundry() ######################
calc_boundry() {
ips=$((2 ** (32 - $2)))
if [ $ips -gt 256 ]
then
step=256
else
step=$ips
fi
hosts=$((2 ** ($1 - $2)))
boundry=$((($3 / $hosts) * $hosts))
wildcard=$(((2 ** ($1 - $2)) - 1))
}
##########################################################
RED="\e[91m"
NC="\e[0m"
chk_os_version $(cat /etc/*-release | grep -i centos | wc -l)
netmask=255.255.255.255
echo
echo -e "${RED}>>>${NC} Please enter IP(s) with netmask (e.g. 192.168.100.0/25) ${RED}<<<${NC}"
echo -e "${RED}>>>${NC} Each new IP ranges please write from the new line ${RED}<<<${NC}"
echo -e "${RED}>>>${NC} When you done, hit Ctrl-D on a blank line to stop input ${RED}<<<${NC}"
echo
readarray -t subnet
echo
for ((i=0; i < ${#subnet[@]}; i++))
do
chk_subnet_input ${subnet[i]}
done
for ((i=0; i < ${#subnet[@]}; i++))
do
mask=$(echo ${subnet[i]} | awk -F '/' '{print $2}')
ipaddr=$(echo ${subnet[i]} | awk -F '/' '{print $1}')
oct1=$(echo $ipaddr | awk -F '.' '{print $1}')
oct2=$(echo $ipaddr | awk -F '.' '{print $2}')
oct3=$(echo $ipaddr | awk -F '.' '{print $3}')
oct4=$(echo $ipaddr | awk -F '.' '{print $4}')
case $mask in
3[0-1]|2[4-9])
calc_boundry 32 $mask $oct4
ipaddr=$(echo $ipaddr | cut -d '.' -f1-3)
ipaddr_start=${ipaddr}.${boundry}
oct4=$(($boundry + $wildcard))
ipaddr_end=${ipaddr}.${oct4}
upd_clonenum_start
add_ifcfg-range
;;
2[0-3]|1[6-9])
calc_boundry 24 $mask $oct3
ipaddr=$(echo $ipaddr | cut -d '.' -f1-2)
while [ $wildcard -ge 0 ]
do
ipaddr_start=${ipaddr}.${boundry}.0
ipaddr_end=$(echo $ipaddr_start | cut -d '.' -f1-2)
ipaddr_end=${ipaddr_end}.${boundry}.255
upd_clonenum_start
add_ifcfg-range
let boundry++
let wildcard--
done
;;
esac
done
/etc/init.d/network restart
echo