Shell script implementation automatically modifies IP address _linux shell

Source: Internet
Author: User
Tags mkdir

As a Linux SA, many parts of the day-to-day operation of the script, and the server's IP is generally static IP or Mac binding, of course, the latter is relatively cumbersome to operate, and the former we can set the host name, IP information, gateways and other configuration. Modification to a specific hostname is also more convenient for maintenance and management. The following scripting uses: To modify the IP and host name and other related information, can be modified according to actual needs, extrapolate!

#!/bin/sh #auto Change IP netmask gateway scripts #wugk 2012-12-17 Cat << EOF ++++++++ automatically modify related information such as IP and host name +++++++++ 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 defines variables that can be based on the actual Inter-State modification Ethconf=/etc/sysconfig/network-scripts/ifcfg-eth0 hosts=/etc/hosts network=/etc/sysconfig/network DIR=/dat a/backup/' Date +%y%m%d ' netmask=255.255.255.0 echo "================================================" Echo #定义chang e_ip function Change_ip () {#判断备份目录是否存在, with spaces before and after brackets! The exclamation point in the shell represents the opposite meaning # if [!-D $DIR];then mkdir-p $DIR fi echo ' Now-change IP address, doing Backup Interface Eth0 "CP $ETHCONF $DIR grep" DHCP "$ETHCONF #如下 $? Used to determine the state of the last operation, 0, indicating that the last operation was correct or successful # if [$-eq 0];then #read -P Interactive input variable ipaddr, with a space after the colon, sed-i Modify profile # read-p "Please insert IP address:" IPAddr sed-i ' s/dhcp/static/g ' $ETHCONF #awk-F. is to separate the field with a., print the first three columns # echo-e "ipaddr= $IPADDR \nnetmask= $NETMASK \ngateway= ' echo $IPADDR |awk-f. ' {print $. ' $ "." 
 
$} '. 254 ' >> $ETHCONF echo ' This IP address change success! " else Echo-n "This $ETHCONF be 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 [@]} to get the total number of variable values # a=${#count [@]} #while条件语句判断, the number is correct, incorrect loop prompts input, you can use [0-9] to determine 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. ' * prin T $1,$2,$3,$4} ' a=${#count [@]} done #sed-e can continuously modify multiple parameters # Sed-i-E ' s/^ipaddr/#IPADDR/g '-e ' s/^netmask/#NETMASK /g '-e ' s/^gateway/#GATEWAY/g ' $ETHCONF #echo-e \ n for continuous append and Auto wrap # echo-e ipaddr= $IPADDR \nnetmask= $NETMASK \ngateway= ' echo $IPADDR |awk-f. ' {print $. ' $ "." $} '. 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 the change success" Else echo "This $host is Exist ..." fi} ########## #fuction Network ############### #定义network函数 function Change_network () {if [!-D $DIR];then mkdir-p $DIR fi cp $NETW ORK $DIR read-p "Please insert IP address": ipaddr host= ' echo $IPADDR |sed ' s/\./-/g ' grep ' $host ' $NETWORK i F [$?-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为菜单选择命令, Format select $var in.. command.. Do ... select I in "Change_ip" "change_hosts" "change_network" doing #case method, generally used for judging cases under multiple conditions $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
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.