#!/bin/bash
IFFILE='/root/ifcfg-eth0'
read -p "IPaddr[192.168.0.1]:" MYIP
[ -z $MYIP ] && MYIP='192.168.0.1'
echo "Please choose an option[1,2,3]:"
cat << EOF
1)255.255.255.0
2)255.255.0.0
3)255.0.0.0
EOF
read CHOICE
case $CHOICE in
'1')
MYMASK='255.255.255.0' ;;
'2')
MYMASK='255.255.0.0' ;;
'3')
MYMASK='255.0.0.0' ;;
*)
MYMASK='255.255.255.0' ;;
esac
[ -z $MYMASK ] && MYMASK=255.255.255.0
grep -E "^BOOTPROTO=(dhcp)|(bootp)$" $IFFILE &> /dev/null && sed -i "s/^BOOTPROTO=.*/BOOTPROTO=static/g" $IFFILE
grep "^IPADDR=.*" $IFFILE &> /dev/null && sed -i "s/IPADDR=.*/IPADDR=$MYIP/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \IPADDR=$MYIP" $IFFILE
grep "^NETMASK=.*" $IFFILE &> /dev/null && sed -i "s/NETMASK=.*/NETMASK=$MYMASK/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \NETMASK=$MYMASK" $IFFILE
转载于:https://blog.51cto.com/jason123/469294