- #!/bin/sh
- read -p "请输入网卡设备名查看目前网卡配置,例如eth0 " eth
- if [ -z "${eth}" ]
- then
- echo -e "\e[1;32m必须输入网卡名称\e[0m"
- exit
- else
- ifconfig $eth |sed -ne 's/^.* inet addr:\([^ ]*\).* Bcast:\([^ ]*\).* Mask:\(.*$\)/IP:\1\nBcast:\2\nMASK:\3/gp'
- read -p "是否替换?yes/no? " select
- if [ ${select} = 'yes' ]
- then
- read -p "请输入新的ip 不填写则为空地址 " ip
- read -p "请输入新的网络掩码 不填写则为空地址 " mask
- read -p "请输入新的网关 不填写则为空地址 " gw
- ifcfg=/etc/sysconfig/network-scripts/ifcfg-${eth}
- cp ${ifcfg} ${ifcfg}.bak
- gwadd=`grep GATEWAY /etc/sysconfig/network-scripts/ifcfg-${eth}`
- ifdown ${eth}
- if [ $? -eq 0 ]
- then
- sed -i "s/^IPADDR=.*/IPADDR=${ip}/g" $ifcfg
- sed -i "s/^NETMASK=.*/NETMASK=${mask}/g" $ifcfg
- sed -i "s/ONBOOT=no/ONBOOT=yes/g" $ifcfg
- if [ -n "${gw}" ]
- then
- ${gwadd}
- if [ $? -eq 0 ]
- then
- sed -i "s/^GATEWAY.*/GATEWAY=${gw}/g" $ifcfg
- else
- echo -e "\e[1;32mGATEWAY=${gw}\e[0m" >> $ifcfg
- fi
- route add default gw ${gw}
- else
- echo -e "\e[1;32m你没有输入网关,网关将设置为空\e[0m"
- fi
- else
- echo -e "\e[1;32m对不起, ${eth} 关闭失败\e[0m"
- exit 1
- fi
- ifup ${eth}
- ping -c 4 "${gw}" &
- if [ $? -eq 0 ]
- then
- echo -e "\e[1;32mIP修改成功!\e[0m"
- else
- echo -e "\e[1;32mIP修改失败!\e[0m"
- fi
- else
- echo -e "\e[1;32m你没有选择替换!拜拜!\e[0m"
- fi
- fi