用户名: 密码: 忘记密码? 注册

IPADD shell编程

作者:  时间: 2010-12-22
脚本:判断 ‘/etc/sysconfig/network-scripts/ifcfg-eth0’
 
BOOTPROTO=(dhcp)|(bootp) 变为static
IPADDR=
NETMASK=
 
 
程序如下:
#!/bin/bash
 
FILE="/etc/sysconfig/network-scripts/ifcfg-eth0"
 
read -P "Please input a ipaddress[192.168.0.1]:" MYIP
[ -z $MYIP ] && MYIP='192.168.0.1'
 
echo "Please input a netmask[255.255.255.0]:"
cat << EOF
1) 255.255.255.0
2) 255.255.0.0
3) 255.0.0
EOF
 
read CHOICE
 
case $CHOICE in
  '1')
    MMASK='255.255.255.0' 
    ;;
  '2')
    MMASK='255.255.0.0'
    ;;
  '3')
    MMASK='255.0.0.0'
    ;;
  '*')
    [ -z $CHOICE ] && echo "Please input correct choice:" || MMASK='255.255.255.0'
    ;;
esac
[ -z $CHOICE ] && MMASK='255.255.255.0'
 
if grep -E "^BOOTPROTO=(dhcp)|(bootp)$" $FILE &>/dev/null ; then
  sed -i 's/^BOOTPROTO=.*/BOOTPROTO=static/g' $FILE
grep “^IPADDR=.*” $FILE &>/dev/null && sed -i "s/IPADDR=.*/IPADDR=$MYIP/g" $FILE || sed -i "/^BOOTPROTO=.*/a \IPADDR=$MYIP" $FILE
grep "^NETMASK=.*" $FILE &>/dev/null && sed -i "s/NETMASK=.*/NETMASK=$MMASK/g" $FILE || sed -i "/^IPADDR=.*/a \NETMASK=$MMASK" $FILE
fi