设置IP地址
#!/bin/bash #Absolute path to this script SCRIPT=$(readlink -f $0) #Absolute path this script is in SCRIPTPATH=$(dirname $SCRIPT) echo $SCRIPT echo $SCRIPTPATH #Initial defaults _NAME=ens33 _LOC_IP=192.168.31.91 _GW_IP=192.168.31.1 _PREFIX=24 clear echo echo echo "This script will help you easily set up IP ADDRESS" echo #check for root user if [ "$(id -u)" -ne 0 ] ; then echo "You must run this script as root. Sorry!" exit 1 fi #判断IP ADDRESS是否合规 if ! echo $LOC_IP | egrep -q '(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ; then #Read the IP read -p "Please select the IP for this instance: [$_LOC_IP] " LOC_IP if ! echo $LOC_IP | egrep -q '(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ; then echo " Selecting default: $_LOC_IP" echo LOC_IP=$_LOC_IP else echo " Selecting IP: $LOC_IP" echo fi fi #判断IP GATEWAY是否合规 if ! echo $GW_IP | egrep -q '(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ; then #Read the IP read -p "Please select the IP GW for this instance: [$_GW_IP] " GW_IP if ! echo $GW_IP | egrep -q '(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ; then echo " Selecting default: $_GW_IP" echo GW_IP=$_GW_IP else echo " Selecting GW: $GW_IP" echo fi fi cat>/etc/sysconfig/network-scripts/ifcfg-$_NAME<<EOF TYPE="Ethernet" BOOTPROTO="static" ONBOOT="yes" DEVICE="$_NAME" NETMASK=$_PREFIX GATEWAY=$GW_IP IPADDR=$LOC_IP DNS1=114.114.114.114 EOF systemctl restart network ifconfig