Linux Shell script automatically modifies the IP address and the ip address of the linuxshell script

Source: Internet
Author: User

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!

 

  1. #! /Bin/sh
  2. # Auto Change ip netmask gateway scripts
  3. # Wugk 2012-12-17
  4. Cat <EOF
  5. ++ Automatically modifies ip address, host name, and other related information.
  6. ETHCONF =/etc/sysconfig/network-scripts/ifcfg-eth0
  7. HOSTS =/etc/hosts
  8. NETWORK =/etc/sysconfig/network
  9. DIR =/data/backup/'date + % Y % m % d'
  10. NETMASK = 255.255.255.0
  11. +++ ++ ------------------------------- ++
  12. EOF
  13. # Define Path defines the variable, which can be modified according to the actual situation
  14.  
  15. ETHCONF =/etc/sysconfig/network-scripts/ifcfg-eth0
  16. HOSTS =/etc/hosts
  17. NETWORK =/etc/sysconfig/network
  18. DIR =/data/backup/'date + % Y % m % d'
  19. NETMASK = 255.255.255.0
  20.  
  21. Echo "============================================== =========="
  22. Echo
  23. # Define the change_ip Function
  24. Function Change_ip ()
  25. {
  26. # Determine whether the backup directory exists. There are spaces before and after brackets ,! The exclamation point in shell indicates the opposite meaning #
  27. If
  28. [! -D $ DIR]; then
  29.  
  30. Mkdir-p $ DIR
  31.  
  32. Fi
  33.  
  34. Echo "Now Change ip address, Doing Backup Interface eth0"
  35. Cp $ ETHCONF $ DIR
  36.  
  37. Grep "dhcp" $ ETHCONF
  38. # $? Used to determine the status of the last operation. The value 0 indicates that the last operation is in correct or successful status #
  39. If
  40.  
  41. [$? -Eq 0]; then
  42. # Read-p Interactive input variable IPADDR. Note that there are spaces after the colon, sed-I modify the configuration file #
  43. Read-p "Please insert ip Address:" IPADDR
  44. Sed-I's/dhcp/static/G' $ ETHCONF
  45. # Awk-F. It means to use the. Number to separate domains and print the first three columns #
  46. Echo-e "IPADDR = $ IPADDR \ nNETMASK = $ NETMASK \ nGATEWAY = 'echo $ IPADDR | awk-F. '{print $1 ". "$2 ". "$3 }''. 254 ">>$ ETHCONF
  47. Echo "This IP address Change success! "
  48.  
  49. Else
  50. Echo-n "This $ ETHCONF is static exist, please ensure Change Yes or NO ":
  51. Read I
  52.  
  53. Fi
  54.  
  55. If
  56. ["$ I" = "y"-o "$ I" = "yes"]; then
  57. Read-p "Please insert ip Address:" IPADDR
  58.  
  59. Count = ('echo $ IPADDR | awk-F. '{print $1, $2, $3, $4 }'')
  60. # Define an array. $ {# count [@]} indicates the total number of retrieved variable values #
  61. A =$ {# count [@]}
  62. # 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 #
  63. While
  64.  
  65. ["$ A"-ne "4"]
  66.  
  67. Do
  68.  
  69. Read-p "Please re Inster ip Address, example 192.168.0.11 ip": IPADDR
  70. Count = ('echo $ IPADDR | awk-F. '{print $1, $2, $3, $4 }'')
  71. A =$ {# count [@]}
  72.  
  73. Done
  74. # Sed-e can be used to modify multiple parameters continuously #
  75. Sed-I-e's/^ IPADDR/# IPADDR/G'-e's/^ NETMASK/# NETMASK/G'-e's/^ GATEWAY/# GATEWAY/ g' $ ETHCONF
  76. # Echo-e \ n is a continuous Append content with a line feed #
  77. Echo-e "IPADDR = $ IPADDR \ nNETMASK = $ NETMASK \ nGATEWAY = 'echo $ IPADDR | awk-F. '{print $1 ". "$2 ". "$3 }''. 254 ">>$ ETHCONF
  78. Echo "This IP address Change success! "
  79. Else
  80. Echo "This $ ETHCONF static exist, please exit"
  81. Exit $?
  82.  
  83. Fi
  84. }
  85. # Define the hosts Function
  86. ########### Function hosts ##############
  87. Function Change_hosts ()
  88. {
  89.  
  90. If
  91.  
  92. [! -D $ DIR]; then
  93. Mkdir-p $ DIR
  94.  
  95. Fi
  96.  
  97. Cp $ HOSTS $ DIR
  98. Read-p "Please insert ip address": IPADDR
  99.  
  100. Host = 'echo $ IPADDR | sed's/\./-/G''
  101. Cat $ HOSTS | grep 127.0.0.1 | grep "$ host"
  102. If
  103. [$? -Ne 0]; then
  104. Sed-I "s/127.0.0.1/127.0.0.1 $ host/g" $ HOSTS
  105. Echo "This hosts change success"
  106.  
  107. Else
  108. Echo "This $ host IS Exist .........."
  109.  
  110. Fi
  111. }
  112.  
  113. ########## Fuction network ###############
  114. # Define network functions
  115. Function Change_network ()
  116. {
  117. If
  118.  
  119. [! -D $ DIR]; then
  120. Mkdir-p $ DIR
  121.  
  122. Fi
  123. Cp $ NETWORK $ DIR
  124. Read-p "Please insert ip address": IPADDR
  125.  
  126. Host = 'echo $ IPADDR | sed's/\./-/G''
  127. Grep "$ host" $ NETWORK
  128.  
  129. If
  130. [$? -Ne 0]; then
  131. Sed-I "s/^ HOSTNAME/# HOSTNAME/g" $ NETWORK
  132. Echo "NETWORK = $ host"> $ NETWORK
  133.  
  134. Else
  135. Echo "This $ host IS Exist .........."
  136.  
  137. Fi
  138. }
  139. # The PS3 is generally a menu prompt #
  140. PS3 = "Please Select ip or hosts Menu ":
  141. # Select command for the menu, in the format of select $ var in... command... do... done
  142. Select I in "Change_ip" "Change_hosts" "Change_network"
  143. Do
  144. # Case method, generally used for judgment under multiple conditions
  145. Case $ I in
  146. Change_ip)
  147. Change_ip
  148. ;;
  149. Change_hosts)
  150. Change_hosts
  151. ;;
  152. Change_network)
  153. Change_network
  154. ;;
  155. *)
  156. Echo
  157. Echo "Please Insert $0: Change_ip (1) | Change_hosts (2) | Change_network (3 )"
  158. Echo
  159. ;;
  160. Esac
  161. Done

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.