Bash-shell-scripts Advanced Script Configuration IP address

Source: Internet
Author: User

[[email protected] shell]# cat mod_ip_hostname.sh #!/bin/bash#auto modify  Ip and hostname#by colin on 2015-05-07ip_config= '/etc/sysconfig/network-scripts/ Ifcfg-eth0 ' hostname_config= '/etc/sysconfig/network ' hosts_config= '/etc/hosts ' #定义一个IP主要内容数组a =0  # Defines the variable a as an array subscript, dynamically used when calling ip_array[a]ip_array= (IPADDRNATMASKGATEWAYDNS1DNS2) #判断IP是否符合标准规则function  judge_ip () {          #这里local  $1 error, using 2>/dev/null to screen out errors, not found to affect the output results          local $1 2>/dev/null         TMP_TXT=/tmp/iptmp.txt        echo $1 >  ${tmp_txt}        ip_addr= ' grep -eo  ' ([0-9]{1,3}\.) {3} [0-9] {1,3} '  ${tmp_txt} '          #判断有没有符合 ip  of the ***.***.***.*** rule        if [ ! -z  "${ip_addr}"  ];then                 local j=0;                  #通过循环来检测每个点之前的数值是否符合要求                  for  ((i=1;i<=4;i++))                  do                          local ip_num= ' echo  "${ip_addr}"  |awk -F.  "{print $" $i "}" '                           #判断IP_NUM是否在0与255之间                          if [  "${ip_num}"  -ge 0 -a  "$ {Ip_num} " -le 255 ];then                                   ((j + +));                         else                                  return 1                         fi                 done                  #通过j的值来确定是否继续匹配规则, Cycle four times, if all are correct j=4.                 if [  "$j"  -eq 4 ]; then                          #确认是否为自己想要输入的IP地址              read -n 1 -p  input ${ip_array[a} has a value of ${ip_addr}, confirm input: y|y; re-enter: R|r: "  OK            case ${OK} in                          y|y)  rm -rf ${TMP_TXT} ; return 0;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;R|R)   return 1;;                 *)  return 1;;             esac                 else                          return 1                 fi        else                 return 1        fi}# Get the correct IP value # by calling the function Judge_ip, the final value of the variable ip_addr is the correct function read_right_ip () {         read -p  "Please enter the value of ${ip_array[a]}:"  IP_ADDRS#        ip_addrs= ""     judge_ip  "${iP_addrs} ";     i= ' echo $?      #循环直到输入正确的IP为止      until [  "$i" &NBSP;-EQ&NBSP;0&NBSP;];d O        echo  -e  "\033[31m\n you entered the wrong ${ip_array[a]} value: ${ip_addrs} ====>>>>\033[0m"           read -p  "re-enter ${ip_array[a]}, enter:"  IP_ADDRS         judge_ip  "${ip_addrs}";         i= ' echo $? '     done} #判断网卡配置文件, exists on backup, does not exist create a new blank file Function chang_ip () {         if [ -f ${IP_CONFIG} ];then                 cp ${IP_CONFIG}  ${ip_config}.bak                  #网卡的MAC地址                 hw_addr= ' grep  ' HWADDR '  ${IP_ CONFIG} '                 # IP Configuration Status                 dhcp_ status= ' grep  ' Bootproto '  ${IP_CONFIG} |awk -F=  ' {print toupper ($)} '                   #如果BOOTPROTO的值是DHCP, Continue to set the IP, or quit it                  if [  "${dhcp_status}"  =  ' DHCP '  ];then                         echo -e   "\033[32MIP get: ${dhcp_status}, the following will be modified to static ip...\033[0m"                          rm -rf ${ip_config} & & touch ${IP_CONFIG}; #把部分基本信息导入到网卡配置文件内cat  >${ip_config} <<eofdevice= eth0hwaddrtype=ethernetonboot=yesbootproto=staticeof##########################################                           #把原来MAC地址写进去                          sed -i  "/hwaddr/s/hwaddr/${ Hw_addr}/g " ${IP_CONFIG}                          #循环五次, call the function five times to get all the data you want to set, respectively                           for  (a=0;a<=4;a++))                          do                                  read_right_IP;                                  echo -e  "\033[32m\n${ip_array[a]}=${ip_addrs}\033[0m"                                    #把内容追加到网卡配置文件的最后                                   echo -e  "${ip_array[a]}=${ip_addrs}"  >> ${IP_CONFIG}                          done                         echo -e  "\033[32m\n+++++++ Set the IP-related information as follows +++++++\033[1m "                         cat ${IP_CONFIG};                          echo -e  "\033[32m\n++++++++++++++++++++++++++++++++++\033[0m"                  else                         echo -e  "\033[ 32m system IP is already: ${dhcp_status}, no need to modify ... \033[0m "                  fi        else                 echo -e  "\033[ 31m NIC profile: ${ip_config} does not exist, please check if the system is corrupted ... \033[0m "         fi}# Script Selection Menu menu= (        chang_ip_config         chang_hostname_config        chang_hosts_confg         exit_menu        help_menu) Ps3= "please select menu will running to do  (Need help,Please  input: 5 ):  "Select i in ${menu[@]}do        case  $i  in                 ${menu[0]})                           chang_ip                         if [  "$?"  -eq 0 ];then                                  echo -e  "\033[32m modify IP for static get success ... \033[0m"                           else                                  echo -e  "\033[31m Please check if IP is still getting ... \033[0m"                           fi                 ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;${MENU[1]})  exit ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;${MENU[2]})  exit ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;${MENU[3]})  exit ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;${MENU[4]})                         echo -e  "\033[32m========= Help menu contents are as follows ==========\033[ 0m "                          for  ((i=0;i< "${#menu [@]}"; i++)                           do                                 echo  -e  "\033[33m  ' expr  $i  + 1 ')  ${menu[i]} \033[0m"                           done                 ;;          esacdone 

For more information, please follow: shell-scripts script to modify IP address

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>http://www.jfedu.net/ forum.php?mod=viewthread&tid=1776&fromuid=100
(Source: Linux operation and maintenance training-Keio Linux education website)

The script runs as shown:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6C/92/wKioL1VMbDXhVPB2AAHTI9UA_Lc233.jpg "title=" Qq20150508152904.jpg "alt=" Wkiol1vmbdxhvpb2aahti9ua_lc233.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/91/wKioL1VMajvShnxRAANm2zLLeq0615.jpg "style=" float: none; "title=" qq20150508153656.jpg "alt=" Wkiol1vmajvshnxraanm2zlleq0615.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/96/wKiom1VMaMmAIOExAANMwSoxf-Q630.jpg "style=" float: none; "title=" qq20150508153715.jpg "alt=" Wkiom1vmammaioexaanmwsoxf-q630.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/91/wKioL1VMaj6Ddl_bAADivmuDq_8316.jpg "style=" float: none; "title=" qq20150508153750.jpg "alt=" Wkiol1vmaj6ddl_baadivmudq_8316.jpg "/>


This article is from the "Mountain Road 18 Bend" blog, please be sure to keep this source http://kongzi68.blog.51cto.com/1432619/1649605

Bash-shell-scripts Advanced Script Configuration IP address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.