Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1172765
  • 博文数量: 150
  • 博客积分: 2739
  • 博客等级: 少校
  • 技术积分: 2392
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-07 12:28
文章分类

全部博文(150)

文章存档

2015年(2)

2014年(16)

2013年(10)

2012年(58)

2011年(64)

分类: Python/Ruby

2011-09-27 15:20:10

#!/bin/bash
#
TMPFILE=`mktemp /tmp/eth.XXXXXXXX`

read -p "Interface: " ETHCARD

ALLECARD=`ifconfig -a | awk '/^[^[:space:]l]/{print $1}'`

until echo $ALLECARD | grep "$ETHCARD" &> /dev/null; do
  echo -e "\033[31mWrong Card name.\033[0m"
  read -p "Interface: " ETHCARD
done
 
ETHFILE=/etc/sysconfig/network-scripts/ifcfg-$ETHCARD

read -p "Boot Protocol: " MYBOOTPROTO
until echo $MYBOOTPROTO | grep -E "dhcp|bootp|static|none"; do
  echo -e "\033[31mWrong BOOTPROTO.\033[0m"
  read -p "Boot Protocol: " MYBOOTPROTO
done

if [ "$MYBOOTPROTO" == "dhcp" ]; then
  sed -i "s/^BOOTPROTO=.*/BOOTPROTO=dhcp/g" $ETHFILE
  if [ $? -eq 0 ]; then
    ifdown $ETHCARD && ifup $ETHCARD
    [ $? -eq 0 ] && echo "Set $ETHCARD done."
  fi
elif [ "$MYBOOTPROTO" == "static" ]; then
  cat $ETHFILE > $TMPFILE
  read -p "Ip Address: " MYIP
  read -p "Netmask: " MYNM
  read -p "Gateway: " MYGW

  sed -i "s/^BOOTPROTO=.*/BOOTPROTO=static/g" $TMPFILE
  grep "^IPADDR=" $TMPFILE &> /dev/null  && sed -i "s/IPADDR=.*/IPADDR=$MYIP/" $TMPFILE || echo "IPADDR=$MYIP" >> $TMPFILE
  grep "^NETMASK=" $TMPFILE &> /dev/null && sed -i "s/NETMASK=.*/NETMASK=$MYNM/" $TMPFILE || echo "NETMASK=$MYNM" >> $TMPFILE

  if [ -z $MYGW ]; then
     sed -i '/^GATEWAY=.*/d' $TMPFILE
  else
     grep "^GATEWAY=" $TMPFILE &> /dev/null && sed -i "s/GATEWAY=.*/GATEWAY=$MYGW/" $TMPFILE || echo "GATEWAY=$MYGW" >> $TMPFILE
  fi
  cp -f $TMPFILE $ETHFILE
  ifdown $ETHCARD && ifup $ETHCARD
  [ $? -eq 0 ] && echo "Set $ETHCARD done."
else 
  echo "No such options."
  exit 1
fi
   
rm -f $TMPFILE

阅读(1007) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~