Bootstrap

shell脚本-静态网络配置

此脚本配置以dhcp的原来的配置为主

输入后的样板:

您的选择如下:

不再添加dns

再添加一个dns:

#!/bin/bash
yum -y install ifconfig &>/dev/null
read -p "请输入网卡名称:" ka
NETMASK=`ifconfig $ka | grep netmask | awk '{print $4}'`
# 获取ip
IP=`ip a | grep "inet.*$ka" | awk '{print $2}' | awk -F'/' '{print $1}'`
# 静态
sed -i 's/BOOTPROTO=.*/BOOTPROTO="static"/' /etc/sysconfig/network-scripts/ifcfg-$ka
# 添加静态ip
echo "IPADDR=$IP" >> /etc/sysconfig/network-scripts/ifcfg-$ka
# 配置子网掩码
echo "NETMASK=$NETMASK" >> /etc/sysconfig/network-scripts/ifcfg-$ka
read -p "请输入网关:" GATEWAY
# 配置网关
echo "GATEWAY=$GATEWAY" >> /etc/sysconfig/network-scripts/ifcfg-$ka
# 添加DNS
echo "DNS1=8.8.8.8" >> /etc/sysconfig/network-scripts/ifcfg-$ka
echo "DNS2=114.114.114.114" >> /etc/sysconfig/network-scripts/ifcfg-$ka
read -p "已自动配置两个dns,请选择是否要继续添加(y/n): " dns
if [ "$dns" = "y" ]; then
   read -p "请输入一个您想添加的DNS地址:" xdns
   echo "DNS3=$xdns" >> /etc/sysconfig/network-scripts/ifcfg-$ka
   echo "dns添加成功!"
else
        echo "正在重启服务中..."
fi
# 重启服务
systemctl restart network
# 测试:
ping -c3 www.jd.com &>/dev/null
if [ $? -eq 0 ];then
        echo "配置成功!"
else
        echo "配置失败"
fi
;