脚本会提示你输入新的 IP 地址,并自动修改 ifcfg-eth0 的配置,设置网络地址,网关地址,DNS 地址,然后重启网络服务。
#!/bin/bash
#
#
#
# set new IP in ifcfg-eth0, /etc/resolve.conf
#
IF_ETH0="/etc/sysconfig/network-scripts/ifcfg-eth0"
HOST_CNF="/etc/sysconfig/network"
DNS_CNF="/etc/resolve.conf"
# set new IP, dns
read -p "please input a new IP: " NEW_IP
NETWORK=`echo $NEW_IP | cut -d'.' -f1-3`.0
GATEWAY=`echo $NEW_IP | cut -d'.' -f1-3`.1
DNS=`echo $NEW_IP | cut -d'.' -f1-3`.1
echo "NEWIP: $NEW_IP"
echo "NEW Network: $NETWORK"
echo "NEW Gateway: $GATEWAY"
echo "New DNS: $DNS"
sed -i "s/IPADDR=.*/IPADDR=$NEW_IP/" $IF_ETH0
sed -i "s/NETWORK=.*/NETWORK=$NETWORK/" $IF_ETH0
sed -i "s/GATEWAY=.*/GATEWAY=$GATEWAY/" $IF_ETH0
sed -i "s/nameserver.*/nameserver $DNS/" /etc/resolv.conf
# set new hostname
#read -p "please input a new hostname: " NEW_HOST
#sed -i "s/HOSTNAME=.*/HOSTNAME=/"
#echo "new hostname is set to :"
#grep "HOSTNAME"
#echo "hostname update will be in effective after restart"
# restart network
echo "restart network"
service network restart
# restart ?
#read -p "restart now? [yes|no]" RESTART
#if [ "yes" = ]; then
#shutdown -r now
#else
#echo "init work done, a restart is needed for new HOSTNAME in effective"
#fi