Automatic IP address modification using Shell scripts

Source: Internet
Author: User

As a Linux SA, scripts are used in many aspects of daily O & M, while Server ip addresses are generally bound to static ip addresses or MAC addresses. Of course, the latter is relatively cumbersome to operate, 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. ETHCONF =/etc/sysconfig/network-scripts/ifcfg-eth0

  15. HOSTS =/etc/hosts

  16. NETWORK =/etc/sysconfig/network

  17. DIR =/data/backup/'date + % Y % m % d'

  18. NETMASK = 255.255.255.0

  19. Echo "============================================== =========="

  20. Echo

  21. # Define the change_ip Function

  22. Function Change_ip ()

  23. {

  24. # Determine whether the backup directory exists. There are spaces before and after brackets ,! The exclamation point in shell indicates the opposite meaning #

  25. If

  26. [! -D $ DIR]; then

  27. Mkdir-p $ DIR

  28. Fi

  29. Echo "Now Change ip address, Doing Backup Interface eth0"

  30. Cp $ ETHCONF $ DIR

  31. Grep "dhcp" $ ETHCONF

  32. # $? Used to determine the status of the last operation. The value 0 indicates that the last operation is in correct or successful status #

  33. If

  34. [$? -Eq 0]; then

  35. # Read-p Interactive input variable IPADDR. Note that there are spaces after the colon, sed-I modify the configuration file #

  36. Read-p "Please insert ip Address:" IPADDR

  37. Sed-I's/dhcp/static/G' $ ETHCONF

  38. # Awk-F. It means to use the. Number to separate domains and print the first three columns #

  39. Echo-e "IPADDR = $ IPADDR \ nNETMASK = $ NETMASK \ nGATEWAY = 'echo $ IPADDR | awk-F. '{print $1 ". "$2 ". "$3 }''. 254 ">>$ ETHCONF

  40. Echo "This IP address Change success! "

  41. Else

  42. Echo-n "This $ ETHCONF is static exist, please ensure Change Yes or NO ":

  43. Read I

  44. Fi

  45. If

  46. ["$ I" = "y"-o "$ I" = "yes"]; then

  47. Read-p "Please insert ip Address:" IPADDR

  48. Count = ('echo $ IPADDR | awk-F. '{print $1, $2, $3, $4 }'')

  49. # Define an array. $ {# count [@]} indicates the total number of retrieved variable values #

  50. A =$ {# count [@]}

  51. # 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 #

  52. While

  53. ["$ A"-ne "4"]

  54. Do

  55. Read-p "Please re Inster ip Address, example 192.168.0.11 ip": IPADDR

  56. Count = ('echo $ IPADDR | awk-F. '{print $1, $2, $3, $4 }'')

  57. A =$ {# count [@]}

  58. Done

  59. # Sed-e can be used to modify multiple parameters continuously #

  60. Sed-I-e's/^ IPADDR/# IPADDR/G'-e's/^ NETMASK/# NETMASK/G'-e's/^ GATEWAY/# GATEWAY/ g' $ ETHCONF

  61. # Echo-e \ n is a continuous Append content with a line feed #

  62. Echo-e "IPADDR = $ IPADDR \ nNETMASK = $ NETMASK \ nGATEWAY = 'echo $ IPADDR | awk-F. '{print $1 ". "$2 ". "$3 }''. 254 ">>$ ETHCONF

  63. Echo "This IP address Change success! "

  64. Else

  65. Echo "This $ ETHCONF static exist, please exit"

  66. Exit $?

  67. Fi


  68. }


  69. # Define the hosts Function

  70. ########### Function hosts ##############

  71. Function Change_hosts ()

  72. {

  73. If

  74. [! -D $ DIR]; then

  75. Mkdir-p $ DIR

  76. Fi

  77. Cp $ HOSTS $ DIR

  78. Read-p "Please insert ip address": IPADDR

  79. Host = 'echo $ IPADDR | sed's/\./-/G''

  80. Cat $ HOSTS | grep 127.0.0.1 | grep "$ host"

  81. If

  82. [$? -Ne 0]; then

  83. Sed-I "s/127.0.0.1/127.0.0.1 $ host/g" $ HOSTS

  84. Echo "This hosts change success"

  85. Else

  86. Echo "This $ host IS Exist .........."

  87. Fi

  88. }

  89. ########## Fuction network ###############

  90. # Define network functions

  91. Function Change_network ()

  92. {

  93. If

  94. [! -D $ DIR]; then

  95. Mkdir-p $ DIR

  96. Fi

  97. Cp $ NETWORK $ DIR

  98. Read-p "Please insert ip address": IPADDR

  99. Host = 'echo $ IPADDR | sed's/\./-/G''

  100. Grep "$ host" $ NETWORK

  101. If

  102. [$? -Ne 0]; then

  103. Sed-I "s/^ HOSTNAME/# HOSTNAME/g" $ NETWORK

  104. Echo "NETWORK = $ host"> $ NETWORK

  105. Else

  106. Echo "This $ host IS Exist .........."

  107. Fi

  108. }


  109. # The PS3 is generally a menu prompt #

  110. PS3 = "Please Select ip or hosts Menu ":

  111. # Select command for the menu, in the format of select $ var in... command... do... done

  112. Select I in "Change_ip" "Change_hosts" "Change_network"


  113. Do

  114. # Case method, generally used for judgment under multiple conditions

  115. Case $ I in

  116. Change_ip)

  117. Change_ip

  118. ;;

  119. Change_hosts)

  120. Change_hosts

  121. ;;

  122. Change_network)

  123. Change_network

  124. ;;

  125. *)

  126. Echo

  127. Echo "Please Insert $0: Change_ip (1) | Change_hosts (2) | Change_network (3 )"

  128. Echo

  129. ;;

  130. Esac


  131. 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.