设置ip信息的shell脚本

发布时间:2019-12-29编辑:脚本学堂
设置ip信息的shell脚本

设置ip信息的shell/ target=_blank class=infotextkey>shell脚本,感兴趣的朋友可以研究下。
 

复制代码 代码如下:

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

read -p "Interface: " ETHCARD

ALLECARD=`ifconfig -a | linuxjishu/13830.html target=_blank class=infotextkey>awk '/^[^[:space:]l]/{print $1}'`

until echo $ALLECARD | grep "$ETHCARD" &> /dev/null; do
  echo -e "33[31mWrong Card name.33[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 "33[31mWrong BOOTPROTO.33[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