設定Linux伺服器IP地址的shell指令碼
echo "------SET IP NETMASK GATEWAY-----"
read -p "IPADDR: " gkAdd
read -p "NETMASK: " gkMask
read -p "GATEWAY: " gkWay
cp /etc/sysconfig/network-scripts/ifcfg-enp1s0 /etc/sysconfig/network-scripts/ifcfg-enp1s0.bak
cp /etc/sysconfig/network-scripts/ifcfg-enp2s0 /etc/sysconfig/network-scripts/ifcfg-enp2s0.bak
sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/' /etc/sysconfig/network-scripts/ifcfg-enp1s0
sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/' /etc/sysconfig/network-scripts/ifcfg-enp2s0
sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-enp1s0
sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "IPADDR="${gkAdd} >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "NETMASK="${gkMask} >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "GATEWAY="${gkWay} >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "DNS1=10.188.0.55" >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "DNS2=10.188.0.56" >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "IPADDR="${gkAdd} >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "NETMASK="${gkMask} >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "GATEWAY="${gkWay} >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "DNS1=10.188.0.55" >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "DNS2=10.188.0.56" >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
systemctl restart network
ping -c5 ${gkWay} >/dev/null 2>&1
if [ $? -eq 0 ]
then
echo "netWork OK."
else
echo "netWork failed,try again."
fi