-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_staticIP.sh
51 lines (43 loc) · 962 Bytes
/
replace_staticIP.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
#!/usr/bin/env bash
set -eu
# set -x
set -o pipefail
# CONFIGFILE=/tmp/ifcfg-ens33
CONFIGFILE=/etc/sysconfig/network-scripts/ifcfg-ens33
function usage() {
cat >&2 <<-'EOF'
USAGE:
-i --ip(required): ip address
-m --mask: NETMASK
-g --gateway: GATEWAY
EOF
}
DEFAULTIP=''
DEFAULTMASK=255.255.255.0
DEFAULTGATEWAY=192.168.160.2
while getopts "i:m:g:" opt; do
case ${opt} in
i )
ip=${OPTARG}
;;
m )
mask=${OPTARG}
;;
g )
gateway=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
IP=${ip:-${DEFAULTIP}}
MASK=${mask:-${DEFAULTMASK}}
GATEWAY=${gateway:-${DEFAULTGATEWAY}}
if [[ -z ${IP} ]]; then
echo "IP must be passed"
usage
exit 1
fi
sed -i "s/^BOOTPROTO.*/BOOTPROTO=static/" ${CONFIGFILE}
sed -i "4a NETMASK=${MASK}" ${CONFIGFILE}
sed -i "4a GATEWAY=${GATEWAY}" ${CONFIGFILE}
sed -i "4a IPADDR=${IP}" ${CONFIGFILE}