#!/bin/bash #2017-7-13 #by ip_file=/etc/sysconfig/network-scripts/ifcfg-eth0 zt=`grep "static" $ip_file|wc -l` Bf_file=/data/ifcfg-eth/`date "+%y_%m_%d"`/ IPADDR="192.168.25.107" netmask="255.255.255.0" getaway="192.168.25.2" #备份原来的配置文件 #Back up the original configuration file if [ ! -d $Bf_file ];then mkdir -p $Bf_file fi cp $ip_file $Bf_file #修改IP #change ip if [ $zt -eq 1 ];then sed -i s#IPADDR.*#IPADDR=$IPADDR#g $ip_file sed -i s#NETMASK.*#NETMASK=$netmask#g $ip_file sed -i s#GATEWAY.*#GATEWAY=$getaway#g $ip_file else sed -i s#BOOTPROTO.*#BOOTPROTO=static#g $ip_file cat >>$ip_file<< EOF IPADDR=$IPADDR NETMASK=$netmask GATEWAY=$getaway EOF fi #重启服务并打印配置文件 #Restart the service and print the configuration file sleep 2 service network restart echo "_________________________________" cat $ip_file echo "_________________________________" ifconfig echo "________________________________" ifconfig eth0
转载于:https://blog.51cto.com/tdcqvip/1947248