Linux Shell three automatic modification to static IP

Source: Internet
Author: User

This script is mainly used to modify the IP address to static!

#!/bin/bash# #Target:  auto chmod ip information! #Date:  2015-05-10#author: jacken# Qq:654001593#qq group:170544180#version: 1.0#only allow define variableshopt -s  -o nounset#define variableethconfig= '/etc/sysconfig/network-scripts/ifcfg-eth1 ' HOSTS= '/etc/ Hosts ' network= '/etc/sysconfig/network ' bakdir= "/data/backup/' date +%y%m%d '" netmask= ' 255.255.255.0 ' GATEWAY= ' 192.168.1.254 ' dns1= ' 8.8.8.8 ' ################################################################################# ################################## #Define  function####################################################### ##################################################### #function  change_ip () {#Create  Directoryif  [ ! -d  $BAKDIR  ];thenmkdir -p  $BAKDIRfi #prompt information#backup  network filecp  $ETHCONFIG   $BAKDIR/' date +%y%m%d ' .${ethconfig:37}$$ &&  echo -e  "\e[32mnow change ip address, backup interface ${ethconfig:37} to $ bakdir done\e[0m "#Judge  Get Ip Modeif  ' grep -q  -i   ' DHCP '   $ETHCONFIG '  ;then#Change Get Ip Modesed -i  ' s/dhcp/static/ig '  $ ethconfig sed -i  ' s/^dns/#DNS/ig '    $ETHCONFIGecho  -e  "ipaddr= $IPADDR \ nnetmask= $NETMASK \ngateway= $GATEWAY \ndns1= $DNS 1 " >>  $ETHCONFIG           echo -e  ' \e[32mip change success!\e[0m ' else#warning  informationecho -e  "\e[31mthe network alreday is static,please ensure  yes or no:  " read i                  #Again  Confirmif [  "$i"  ==  "y"  -o  "$i"  = =  "yes"  -o  "$i"  ==  " YES " ];then#Comment Old Configure sed -i -e  ' s/^ipaddr/#IPADDR/g '  -e   ' s/^netmask/#NETMASK/g '  -e  ' s/^gateway/#GATEWAY/g '  -e  ' s/^dns/#DNS/g '  $ ethconfigecho -e  "ipaddr= $IPADDR \nnetmask= $NETMASK \ngateway= $GATEWAY \ndns1= $DNS 1"  >>   $ETHCONFIG  echo -e  "\e[32mthe ip address is  $IPADDR  ,change  success!\e[0m "else#exitecho -e " \e[31mthis  $ETHCONFIG  static exist,  exiting...\e[0m "fifi}################################################################################ #Check  IP ADDRESSFUNCTION CHECK_IP () {read -p  "please input ip address:  " IPADDR#Test IP Formatecho  $IPADDR |grep " ^[0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}$]  > /dev/null 2>&1num=$?if [  $num  -ne 0 ]; thenecho -e  "\e[31mplease&nbsP check ip format exiting....\e[0m "Exit 2else#get part of ip a= ' echo   $IPADDR |awk -f.  ' {print $1} ' b= ' echo  $IPADDR |awk -f.  ' {print $2} ' c= ' echo  $IPADDR |awk -f.  ' {print $3} ' d= ' echo  $IPADDR |awk -f.  ' {print $4} ' ' if [  $a  -gt 255 ] | |   [  $a  -le 0 ];thenecho -e  "\e[31mplease check ip  format exiting...\e[0m "exit 2fiif [  $b  -gt 255 ] | |  [  $b  -lt 0 ];thenecho -e  "\e[31mplease check ip format  exiting...\e[0m "exit 2fiif [  $c  -gt 255 ] | |  [  $c  -lt 0 ];thenecho -e  "\e[31mplease check ip format  exiting...\e[0m "exit 2fiif [  $d  -ge 255 ] | |  [  $d  -le 0 ];thenecho -e  "\e[31mplease check ip format exiting...\e[0m" exit 2fiexport $ Ipaddrfi} #Define  ps3ps3= "Please select your choose:" select i in  "change  IP AND DNS FOR ${ETHCONFIG:37} " " show ${ethconfig:37} config  File "  " restart device ${ethconfig:37} " " Show ${ethconfig:37} status "    "Exit"  do choose= $REPLYcase   $CHOOSE  in 1) check_ipchange_ip;; 2) cat  $ETHCONFIG;; 3) ifdown ${ethconfig:37} > /dev/null 2>&1 && ifup ${ Ethconfig:37} > /dev/null 2>&1if [ $? -eq 0 ];then echo  -e  "\e[32mrestart device ${ethconfig:37} success.\e[0m" elseecho -e  "\e[ 31mrestart device ${ethconfig:37} failure.\e[0m "FI;; 4) ifconfig ${ethconfig:37};; 5) echo -e  ' \e[32mbyebye!\e[0m ' exit; *) echo -e  ' \e[31merror number,please input again! ' Esacdone

Demonstrate:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/A6/wKioL1VPXILiKMC1AAZNrPsODLI760.jpg "title=" 11.jpg "alt=" Wkiol1vpxilikmc1aaznrpsodli760.jpg "/>


This article is from the "Step Into the Linux World" blog, please be sure to keep this source http://linuxnote.blog.51cto.com/9876511/1650076

Linux Shell three automatic modification to static IP

Related Article

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.