#!/bin/bash
! rpm -qa |grep -q net-tools > /dev/null && yum -y install net-tools
path=/etc/sysconfig/network-scripts/ifcfg-$1
ip=`ifconfig $1 |grep inet|head -1|awk '{print $2}'|awk -F "." '{print $1"."$2"."$3}'`.
netmask=255.255.255.0
gateway=192.168.198.2
if `grep -q "dhcp" $path`;then
    sed -ri.bak "/TYPE|BOOT|$1|ONBOOT/ !d" $path
    sed -i "s/dhcp/none/" $path
    sed -i "$ a IPADDR=$ip$2" $path
    sed -i "$ a NETMASK=$netmask" $path
    sed -i "$ a GATEWAY=$gateway" $path
    sed -i "$ a DNS1=114.114.114.114" $path
    sed -i "$ a DNS2=8.8.8.8" $path
else
    sed -ri "s/(IPADDR=$ip)(.*)/\1$2/" $path
fi
ifdown $1;ifup $1

 执行脚本时接网卡名称及修改后的ip