設定Linux伺服器IP地址的shell指令碼

程式設計小白李智障發表於2020-12-03

設定Linux伺服器IP地址的shell指令碼

#!/bin/bash
# 設定LinuxIP
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

# 設定ifcfg-enp1s0網路卡
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

# 設定ifcfg-en2s0網路卡
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

相關文章