Shell指令碼實現自動修改IP地址_linux shell

來源:互聯網
上載者:User

作為一名Linux SA,日常營運中很多地方都會用到指令碼,而伺服器的ip一般採用靜態ip或者MAC綁定,當然後者比較操作起來相對繁瑣,而前者我們可以設定主機名稱、ip資訊、網關等配置。修改成特定的主機名稱在維護和管理方面也比較方便。如下指令碼用途為:修改ip和主機名稱等相關資訊,可以根據實際需求修改,舉一反三!

#!/bin/sh #auto Change ip netmask gateway scripts #wugk 2012-12-17 cat << EOF ++++++++自動修改ip和主機名稱等相關資訊+++++++++ ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0 HOSTS=/etc/hosts NETWORK=/etc/sysconfig/network DIR=/data/backup/`date +%Y%m%d` NETMASK=255.255.255.0 +++++++++-------------------------+++++++++++ EOF #Define Path 定義變數,可以根據實際情況修改   ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0  HOSTS=/etc/hosts  NETWORK=/etc/sysconfig/network  DIR=/data/backup/`date +%Y%m%d`  NETMASK=255.255.255.0  echo "================================================" echo #定義change_ip函數 function Change_ip () { #判斷備份目錄是否存在,中括弧前後都有空格,!歎號在shell表示相反的意思# if  [ ! -d $DIR ];then   mkdir -p $DIR  fi   echo "Now Change ip address ,Doing Backup Interface eth0"  cp $ETHCONF $DIR   grep "dhcp" $ETHCONF #如下$?用來判斷上一次操作的狀態,為0,表示上一次操作狀態正確或者成功# if   [ $? -eq 0 ];then #read -p 互動輸入變數IPADDR,注冒號後有空格,sed -i 修改設定檔#  read -p "Please insert ip Address:" IPADDR  sed -i 's/dhcp/static/g' $ETHCONF #awk -F. 意思是以.號為分隔域,列印前三列#  echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF  echo "This IP address Change success !"  else   echo -n "This $ETHCONF is static exist ,please ensure Change Yes or NO":  read i  fi  if  [ "$i" == "y" -o "$i" == "yes" ];then  read -p "Please insert ip Address:" IPADDR   count=(`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'`)  #定義數組, ${#count[@]}代表擷取變數值總個數#  A=${#count[@]}  #while條件陳述式判斷,個數是否正確,不正確迴圈提示輸入,也可以用[0-9]來判斷ip# while   [ "$A" -ne "4" ]  do   read -p "Please re Inster ip Address,example 192.168.0.11 ip": IPADDR  count=(`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'`)  A=${#count[@]}  done  #sed -e 可以連續修改多個參數#  sed -i -e 's/^IPADDR/#IPADDR/g' -e 's/^NETMASK/#NETMASK/g' -e 's/^GATEWAY/#GATEWAY/g' $ETHCONF  #echo -e \n為連續追加內容,並自動換行#  echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF  echo "This IP address Change success !" else  echo "This $ETHCONF static exist,please exit"  exit $?  fi  }  #定義hosts函數 ############function hosts############## function Change_hosts () {  if   [ ! -d $DIR ];then  mkdir -p $DIR  fi   cp $HOSTS $DIR  read -p "Please insert ip address": IPADDR   host=`echo $IPADDR|sed 's/\./-/g'`  cat $HOSTS |grep 127.0.0.1 |grep "$host"   if  [ $? -ne 0 ];then  sed -i "s/127.0.0.1/127.0.0.1 $host/g" $HOSTS  echo "This hosts change success "  else  echo "This $host IS Exist .........."  fi  } ###########fuction network############### #定義network函數 function Change_network () {  if   [ ! -d $DIR ];then  mkdir -p $DIR   fi  cp $NETWORK $DIR  read -p "Please insert ip address": IPADDR   host=`echo $IPADDR|sed 's/\./-/g'`  grep "$host" $NETWORK   if  [ $? -ne 0 ];then  sed -i "s/^HOSTNAME/#HOSTNAME/g" $NETWORK  echo "NETWORK=$host" >>$NETWORK  else  echo "This $host IS Exist .........."   fi  }  #PS3一般為菜單提示資訊#  PS3="Please Select ip or hosts Menu": #select為菜單選擇命令,格式為select $var in ..command.. do .... done  select i in "Change_ip" "Change_hosts" "Change_network"  do #case 方式,一般用於多種條件下的判斷 case $i in   Change_ip )   Change_ip ;;   Change_hosts )   Change_hosts ;;   Change_network )   Change_network ;;   *)   echo   echo "Please Insert $0: Change_ip(1)|Change_hosts(2)|Change_network(3)"   echo ;; esac  done
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.