Shell script implementation automatically modify IP, host name and other functions to share _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 comparison is relatively cumbersome, 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!

Copy Code code as follows:

#!/bin/sh
#auto Change IP netmask Gateway scripts
#wugk 2012-12-17
Cat <<eof
++++++++ automatically modify the IP and host name and other related information +++++++++
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 modified according to the actual situation
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 ()
{
#判断备份目录是否存在, there are spaces before and after the brackets! The exclamation point in the shell means the opposite.
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, after the colon has a space, sed-i modify the configuration file #
Read-p "Please insert IP address:" IPAddr
Sed-i ' s/dhcp/static/g ' $ETHCONF
#awk-F. To divide the field by the. Number, 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 are 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 prompt input, also can use [0-9] to Judge ip#
While
["$A"-ne "4"]
Todo
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 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 wrap line #
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 are 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 are Exist .... ..."
Fi
}

#PS3一般为菜单提示信息 #
ps3= "Please select IP or hosts menu":
#select为菜单选择命令, the format is select $var in.. command ... Do .... Done
Select I in "Change_ip" "change_hosts" "Change_network"

Todo
#case method, generally used for judgment under various conditions
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

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.