-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_network.sh
335 lines (294 loc) · 10.8 KB
/
setup_network.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
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
#!/bin/bash
#############################################
# Purpose: sets up the network for a newly imaged
# or moved server.
# Author: re-written 2012/06/06 SDW
# Revision: $Rev$
# Updated by: $Author$
# Last change date: $LastChangedDate$
# SVN URL: $HeadURL$
#############################################
##################VARIABLE DEFINITIONS#############
RETCODE=0
SCRIPTDIR1=/maint/scripts
SCRIPTDIR2=/usr/sbin
# Locate the "Configure Interface" script
#configure_net_interface.sh <Interface> <NEW IPv4 ADDRESS> <NEW IPv4 GATEWAY> <NEW IPv4 NETMASK>
if [[ -s "${SCRIPTDIR1}/configure_net_interface.sh" ]]; then
CFGINT="${SCRIPTDIR1}/configure_net_interface.sh"
elif [[ -s "${SCRIPTDIR2}/configure_net_interface.sh" ]]; then
CFGINT="${SCRIPTDIR2}/configure_net_interface.sh"
else
echo "Critical dependency failure: unable to locate configure_net_interface.sh"
exit 5
fi
# Locate the "IP Bond script" script
if [[ -s "${SCRIPTDIR1}/setup_ip_bonding.sh" ]]; then
MKBOND="${SCRIPTDIR1}/setup_ip_bonding.sh"
elif [[ -s "${SCRIPTDIR2}/setup_ip_bonding.sh" ]]; then
MKBOND="${SCRIPTDIR2}/setup_ip_bonding.sh.sh"
else
echo "Critical dependency failure: unable to locate setup_ip_bonding.sh"
exit 5
fi
# Locate and source common_functions.h
if [[ -s "${SCRIPTDIR1}/common_functions.h" ]]; then
source "${SCRIPTDIR1}/common_functions.h"
elif [[ -s common_functions.h ]]; then
source common_functions.h
else
echo "Critical dependency failure: unable to locate common_functions.h"
exit 5
fi
#####################MAIN EXECUTION START###################
# Read current system values
echo "...Gathering information"
echo ""
PHN=`hostname`
PIP=`f_FindPubIP`
if [[ $PIP != FAILURE ]] && [[ -n $PIP ]]; then
if [[ -n `ifconfig -a | grep $PIP | grep 'Mask:'` ]]; then
PNM=`ifconfig -a | grep $PIP | awk -F'Mask:' '{print $NF}' | head -1`
elif [[ -n `ifconfig -a | grep $PIP | grep 'netmask'` ]]; then
PNM=`ifconfig -a | grep $PIP | awk '{print $4}'`
fi
#PGW=`echo $PIP | awk -F'.' '{print $1"."$2"."$3".1"}'`
PGW=`f_FindDefaultGW`
fi
PUBIF=`f_FindPubIF`
if [[ $PUBIF == FAILURE ]]; then
unset PUBIF
export PUBIF=`f_AskPubIF`
fi
# Show current system values before asking if they should be updated
# (This is in preparation to future updates where these values will
# already be set by the kickstart process.)
if [[ $PIP != FAILURE ]] && [[ -z `echo $PHN | egrep -i 'unnamed|setup000|localhost'` ]]; then
echo "Existing Network Settings Found."
echo ""
echo " Hostname: $PHN"
echo " IPv4 Address: $PIP"
echo " Netmask: $PNM"
echo " Gateway: $PGW"
echo ""
read -p "Do you want to change these? (y/n): " changexist
if [[ -z `echo $changexist | grep -i "^y"` ]]; then
# If we had existing settings, maybe we had an existing bond device, but if not...
if [[ -z `/sbin/ifconfig -a | grep "^bond"` ]]; then
# Offer to set up a bond interface with the existing settings
read -p "Do you want to set up bonding with these settings? (y/n): " bondexist
if [[ -n `echo $bondexist | grep -i "^y"` ]]; then
if [[ $PUBIF == eth0 ]] || [[ $PUBIF == eth1 ]]; then
$MKBOND eth0 eth1 bond0
NEEDSRESTART=TRUE
elif [[ $PUBIF == eth2 ]] || [[ $PUBIF == eth3 ]]; then
$MKBOND eth2 eth3 bond0
NEEDSRESTART=TRUE
elif [[ $PUBIF == eth4 ]] || [[ $PUBIF == eth5 ]]; then
$MKBOND eth4 eth5 bond0
NEEDSRESTART=TRUE
elif [[ $PUBIF == eth6 ]] || [[ $PUBIF == eth7 ]]; then
$MKBOND eth6 eth7 bond0
NEEDSRESTART=TRUE
elif [[ -n `echo $PUBIF | egrep 'bond|virt'` ]]; then
echo "Bonding has already been configured."
else
echo "Unable to determine proper NIC pair to bond, skipping."
fi
if [[ $NEEDSRESTART == TRUE ]]; then
echo "Restarting the netowrk to activate the bond."
/etc/init.d/network restart
modprobe bonding
fi
fi
fi
# Since this is an early exit, but a valid one, let's touch our check file
touch /etc/setup_net_complete
exit
fi
fi
# VC is a flag that says whether we received a valid choice
# It will only be set to true when all questions have been
# answered and validated
VC=FALSE
while [[ $VC != TRUE ]]; do
echo "Updating Network Configuration."
echo ""
#Temporarily suppress console messages to keep
#our output clean
/sbin/sysctl -w kernel.printk="3 4 1 3" 2>&1 | > /dev/null
#Get the new hostname
VC1=FALSE
# Matches a non-qualified RFC 1035 hostname
MATCH1='^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)$'
# Matches a fully qualified RFC 1035 hostname
MATCH2='^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$'
#Check the hostname we got
while [[ $VC1 != TRUE ]]; do
read -p " Enter Host Name: " GNHN
NHN=`echo $GNHN | tr '[:upper:]' '[:lower:]'`
if [[ $NHN =~ $MATCH1 ]] || [[ $NHN =~ $MATCH2 ]]; then
# Valid simple hostname
VC1=TRUE
else
echo " \"$NHN\" is not a valid RFC 1035 hostname."
unset NHN
read -p " Press Enter to try again, or Ctrl+C to exit." JUNK
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el
fi
done
#Get the new IPv4 address
VC2=FALSE
while [[ $VC2 != TRUE ]]; do
read -p " Enter IPv4 Address: " NIP
# If the format of the input is valid...
if [[ `f_ValidIPv4 $NIP` == TRUE ]]; then
unset IPINUSE
# Check to see if the address is in use
/sbin/ifconfig $PUBIF up
sleep 1
/sbin/arping -q -c 2 -w 3 -D -I $PUBIF $NIP
IPINUSE=$?
# If the IP is already in use, ask for a different one
if [[ $IPINUSE != 0 ]]; then
echo " \"$NIP\" is already in use on the network."
unset NIP
read -p " Press Enter to select a different IP." JUNK
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el
else
VC2=TRUE
fi
# If the format of the input is NOT valid...
else
echo " \"$NIP\" is not a valid IPv4 address."
unset NIP
read -p " Press Enter to try again." JUNK
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el
fi
done
#Get the new netmask
# First make a guess at what the netmask should be
GNM=255.255.255.0
VC4=FALSE
while [[ $VC4 != TRUE ]]; do
read -p " Enter IPv4 Netmask [$GNM]: " NNM
if [[ -z $NNM ]]; then
NNM=$GNM
VC4=TRUE
elif [[ `f_ValidIPv4 $NNM` == TRUE ]]; then
VC4=TRUE
else
echo " \"$NNM\" is not a valid IPv4 Netmask."
unset NNM
read -p " Press Enter to try again." JUNK
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el
fi
done
#Get the new gateway
# First make a guess at what the gateway should be
GGW=`echo $NIP | awk -F'.' '{print $1"."$2"."$3".1"}'`
VC3=FALSE
while [[ $VC3 != TRUE ]]; do
read -p " Enter IPv4 Gateway [$GGW]: " NGW
if [[ -z $NGW ]]; then
NGW=$GGW
VC3=TRUE
elif [[ `f_ValidIPv4 $NGW` == TRUE ]]; then
VC3=TRUE
else
echo " \"$NGW\" is not a valid IPv4 Gateway."
unset NGW
read -p " Press Enter to try again." JUNK
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el
fi
done
# Display the results we gathered and ask for final verification
unset CONFIRM
#echo -e "\n\n"
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el
echo "The following settings are about to be applied to this system."
echo ""
echo " Hostname: $NHN"
echo " IPv4 Address: $NIP"
echo " IPv4 Gateway: $NGW"
echo " IPv4 Netmask: $NNM"
echo ""
read -p "Are these settings correct? (y/n): " CONFIRM
if [[ -n `echo $CONFIRM | grep -i "^y"` ]]; then
VC=TRUE
else
unset NHN NIP NGW NNM
read -p "New settings rejected. Press Enter to start over." JUNK
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el;
fi
done
#Changing the network settings according to user input
#/usr/sbin/chgnet.sh $NIP $NGW $NNM
#$CHGNET $NIP $NGW $NNM
$CFGINT $PUBIF $NIP $NGW $NNM
RETCODE=$?
if [[ $RETCODE != 0 ]]; then
echo "FAILURE: the command:"
echo " \`$CFGINT $PUBIF $NIP $NGW $NNM\`"
echo " has failed. Please investigate and try again."
exit $RETCODE
fi
#echo ""
#echo "Network settings have been changed. The network needs to be re-started "
#echo "for the changes to take effect. If you are connecting via SSH, you will"
#echo "need to re-connect to $NIP when the network is restarted."
#echo ""
echo "Network will restart in 5 seconds: (Ctrl+C to abort)"
f_SpinningCountdown 5
/etc/init.d/network restart
RETCODE=$?
if [[ $RETCODE != 0 ]]; then
echo "FAILURE: Error Restarting Network"
exit $RETCODE
fi
#Changing the hostname
# If this is a new name and new IP for a new machine, then simply add the "self" address.
if [[ -z `egrep "$NHN|$NIP" /etc/hosts` ]]; then
echo "$NIP $NHN $NHN.wic.west.com" >> /etc/hosts
fi
OHN=`hostname`
# Update the hostname
f_RHELChangeHostname $OHN $NHN
if [[ `f_DetectVM` == FALSE ]]; then
#Configure bonding for physical servers
NEEDSRESTART=
if [[ $PUBIF == eth0 ]] || [[ $PUBIF == eth1 ]]; then
$MKBOND eth0 eth1 bond0
NEEDSRESTART=TRUE
elif [[ $PUBIF == eth2 ]] || [[ $PUBIF == eth3 ]]; then
$MKBOND eth2 eth3 bond0
NEEDSRESTART=TRUE
elif [[ $PUBIF == eth4 ]] || [[ $PUBIF == eth5 ]]; then
$MKBOND eth4 eth5 bond0
NEEDSRESTART=TRUE
elif [[ $PUBIF == eth6 ]] || [[ $PUBIF == eth7 ]]; then
$MKBOND eth6 eth7 bond0
NEEDSRESTART=TRUE
elif [[ -n `echo $PUBIF | egrep 'bond|virt'` ]]; then
echo "Bonding has already been configured."
else
echo "Unable to determine proper NIC pair to bond, skipping."
fi
if [[ $NEEDSRESTART == TRUE ]]; then
echo "The network will be restarted to activate bonding"
echo ""
echo "Network will restart in 5 seconds: (Ctrl+C to abort)"
f_SpinningCountdown 5
/etc/init.d/network restart
RETCODE=$?
fi
fi
#Drop a stop file in /etc/ to indicate that the network was successfully set up
#This will prevent the "fix_profile" script from removing the directives from
#root's profile if something went wrong.
if [[ $RETCODE == 0 ]]; then
touch /etc/setup_net_complete
#echo "Network setup is complete."
else
exit $RETCODE
fi