Linux Shell script automatically modifies the IP address and the ip address of the linuxshell script
As a Linux
SA, scripts are used in routine O & M, while static ip addresses or MAC binding are used for server ip addresses. Of course, the latter operations are relatively cumbersome, in the former, we can set the host name, ip address information, gateway, and other configurations. Modification to a specific host name is also convenient for maintenance and management. The following script is used to modify the ip address, host name, and other information as needed!
- #! /Bin/sh
- # Auto Change ip netmask gateway scripts
- # Wugk 2012-12-17
- Cat <EOF
- ++ Automatically modifies ip address, 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 the variable, which 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
- # Define the change_ip Function
- Function Change_ip ()
- {
- # Determine whether the backup directory exists. There are spaces before and after brackets ,! The exclamation point in shell indicates 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 status of the last operation. The value 0 indicates that the last operation is in correct or successful status #
- If
-
- [$? -Eq 0]; then
- # Read-p Interactive input variable IPADDR. Note that there are spaces 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. It means to use the. Number to separate domains and print the first three columns #
- Echo-e "IPADDR = $ IPADDR \ nNETMASK = $ NETMASK \ nGATEWAY = 'echo $ IPADDR | awk-F. '{print $1 ". "$2 ". "$3 }''. 254 ">>$ ETHCONF
- Echo "This IP address Change success! "
-
- Else
- Echo-n "This $ ETHCONF is 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 }'')
- # Define an array. $ {# count [@]} indicates the total number of retrieved variable values #
- A =$ {# count [@]}
- # The while Condition Statement determines whether the number is correct. If the number is incorrect, the system prompts input in a loop. You can also use [0-9] to determine the ip address #
- 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 be used to modify multiple parameters continuously #
- Sed-I-e's/^ IPADDR/# IPADDR/G'-e's/^ NETMASK/# NETMASK/G'-e's/^ GATEWAY/# GATEWAY/ g' $ ETHCONF
- # Echo-e \ n is a continuous Append content with a line feed #
- Echo-e "IPADDR = $ IPADDR \ nNETMASK = $ NETMASK \ nGATEWAY = 'echo $ IPADDR | awk-F. '{print $1 ". "$2 ". "$3 }''. 254 ">>$ ETHCONF
- Echo "This IP address Change success! "
- Else
- Echo "This $ ETHCONF static exist, please exit"
- Exit $?
-
- Fi
- }
- # Define the hosts Function
- ########### 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 ###############
- # Define network functions
- 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
- }
- # The PS3 is generally a menu prompt #
- PS3 = "Please Select ip or hosts Menu ":
- # Select command for the menu, 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 judgment under multiple 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