centos 7 Network 脚本

#!/bin/sh
#主动启动网卡
interface=$(ls /sys/class/net| grep -v "lo" | head -1)
ifup $interface
 
#获取当前网络信息
default_route=$(ip route show)
default_interface=$(echo $default_route | sed -e 's/^.*dev \([^ ]*\).*$/\1/' | head -n 1)
address=$(ip addr show label $default_interface scope global | awk '$1 == "inet" { print $2,$4}')
ip=$(echo $address | awk '{print $1 }')
ip=${ip%%/*}
mask=$(route -n |grep 'U[ \t]' | head -n 1 | awk '{print $3}')
gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}')
dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
 
 
#判断default_interface是否为空
if [ -z $default_interface ]
then
    default_interface=$interface
fi
 
#显示网络信息
echo -e "The current net info [dynamic]"
echo -e "------------------------------------------"
echo -e " device: $default_interface"
echo -e " ipaddr: $ip"
echo -e "netmask: $mask"
echo -e "gateway: $gateway"
echo -e "    dns: $dns"
echo -e "------------------------------------------"
echo -e ""
 
#获取输入
echo -e "Please give static settings for the server"
echo -e "please input ipaddr:[192.168.1.12]"
read newip
echo -e "please input netmask:[255.255.255.0]"
read newnetmask
echo -e "please input gateway:[192.168.1.1]"
read newgateway
echo -e "please input dns:[192.168.1.1]"
read newdns
echo -e "please input hostname:[MyServer]"
read newhostname
echo -e ""
echo -e "Please check you input"
echo -e "------------------------------------------"
echo -e "  ipaddr: $newip"
echo -e " netmask: $newnetmask"
echo -e " gateway: $newgateway"
echo -e "     dns: $newdns"
echo -e "hostname: $newhostname"
echo -e "------------------------------------------"
echo -e "[yes] for submit
echo -e "[ q ] for exit
 
#确认输入
q=""
while [ "$q" != "q" ]
do
    read q
    case $q in
    q)
        break
        ;;
    yes)
        echo "begging..."
        if [ -e /mnt/plus/supervisor_install.sh ] ;
        then
            # 安装supervisor
            /usr/bin/sh /mnt/plus/supervisor_install.sh
        fi
        # 清空在安装阶段从光盘安装的所有文件
        rm -fr /mnt/*
 
        #主机名配置
        echo "$newhostname" > /etc/hostname
 
        #网络配置
        cp /etc/sysconfig/network-scripts/ifcfg-$default_interface /etc/sysconfig/network-scripts/ifcfg-$default_interface.bak
        uuid=$(cat /etc/sysconfig/network-scripts/ifcfg-$default_interface |grep UUID|sed -e 's/"//g')
 
        echo "IPV6INIT=yes"               > /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "IPV6_AUTOCONF=yes"         >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "BOOTPROTO=none"            >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "DEVICE=$default_interface" >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "ONBOOT=yes"                >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "$uuid"                     >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "TYPE=Ethernet"             >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "PROXY_METHOD=none"         >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "BROWSER_ONLY=no"           >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "IPADDR=$newip"             >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "NETMASK=$newnetmask"       >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "GATEWAY=$newgateway"       >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "DNS1=$newdns"              >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "DEFROUTE=yes"              >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "IPV4_FAILURE_FATAL=no"     >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "IPV6_DEFROUTE=yes"         >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
        echo "IPV6_FAILURE_FATAL=no"     >> /etc/sysconfig/network-scripts/ifcfg-$default_interface
 
        #DNS配置
        cp /etc/resolv.conf /etc/resolv.conf.bak
        echo "# Generated by NetworkManager" > /etc/resolv.conf
        echo "nameserver $newdns" >> /etc/resolv.conf
 
        #重启一下网络 使配置生效
        systemctl restart network
        sleep 5
        ping -c 4 www.baidu.com
        if [ $? != 0 ]
        then
             echo -e "Error! Cant link to Internet"
             #break
        fi
 
        #重启系统
        echo ""
        echo "all settings finished"
        echo "now you system will restart"
        sleep 6
        /usr/sbin/reboot now
        break
        ;;
    *)
        echo "please input 'yes' or 'q'"
        ;;
    esac
done

 

posted @ 2019-09-27 15:10  wwweee000  阅读(586)  评论(0编辑  收藏  举报