centos 7 networkスクリプト

7122 ワード

#!/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