modify.sh 1.23 KB
#!/bin/bash

if [[ $# == 3 ]]
then
	type=inner
	ip=$1
	netmask=$2
	whether_reboot=$3
elif [[ $# == 4 ]]
then
	type=out
	ip=$1
	netmask=$2
	gateway=$3
	whether_reboot=$4
else
	echo Modify IP ERROR!
	exit 0
fi

if [[ $type == inner ]]
then
	net=`egrep -o '^auto eth0$' /etc/network/interfaces`
	if [[ $net ]]
	then
		sed -i /"^auto eth0$"/,/"^address.*$"/s/"address.*$"/"address $ip"/ /etc/network/interfaces
		sed -i /"^auto eth0$"/,/"^netmask.*$"/s/"^netmask.*$"/"netmask $netmask"/ /etc/network/interfaces
		sed -i /"^auto eth0$"/,/"^auto eth1"/s/"^gateway"/"#gateway"/ /etc/network/interfaces
	fi
elif [[ $type == out ]]
then
	net=`egrep -o '^auto eth1$' /etc/network/interfaces`
	if [[ $net ]]
	then
		sed -i /"^auto eth1$"/,/"^address.*$"/s/"address.*$"/"address $ip"/ /etc/network/interfaces
		sed -i /"^auto eth1$"/,/"^netmask.*$"/s/"^netmask.*$"/"netmask $netmask"/ /etc/network/interfaces
		sed -i /"^auto eth1$"/,/"^gateway.*$"/s/"^gateway.*$"/"gateway $gateway"/ /etc/network/interfaces
	fi
fi

if [[ $whether_reboot = reboot ]]
then
	reboot_time=`date -d "1 minutes" +%H:%M`
	echo "IP($ip)修改成功,系统将会在$reboot_time重启"
	shutdown -r $reboot_time
else
	echo "IP($ip)修改成功, 请在合适的时间重启系统(reboot -f)"
fi