As a Linux
SA, daily operations in many places will use the script, and the server's IP generally use static IP or Mac binding, of course, the latter is relatively cumbersome to operate, while the former we can set the hostname, IP information, gateway and other configuration. Modification to a specific hostname is also more convenient for maintenance and management. The following script uses for: modify IP and hostname and other related information, can be modified according to the actual needs, extrapolate!
- #!/bin/sh
- #auto Change IP netmask Gateway scripts
- #wugk 2012-12-17
- Cat << EOF
- ++++++++ automatically modifies IP and host name 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 definition variable, 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, which is 0, indicating that the last operation state was correct or successful #
- If
- [$?-eq 0];then
- #read-P Interactive input variable ipaddr, with a space after the colon, sed-i modify the configuration file #
- Read-p "Please insert IP Address:" ipaddr
- Sed-i ' s/dhcp/static/g ' $ETHCONF
- #awk-F. The first three columns are printed by using the. Number as a delimited field
- 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, you can also use [0-9] to Judge 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 can modify multiple parameters consecutively #
- 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 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为菜单选择命令 in the format of select $var in: command. Do .... Done
- Select I in "Change_ip" "change_hosts" "Change_network"
- Do
- #case method, generally used for judging under a variety of 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
Automatic IP modification of Linux shell scripts