Automatic IP modification of Linux shell scripts

Source: Internet
Author: User

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!

  1. #!/bin/sh
  2. #auto Change IP netmask Gateway scripts
  3. #wugk 2012-12-17
  4. Cat << EOF
  5. ++++++++ automatically modifies IP and host name 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 definition variable, 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. #定义change_ip函数
  22. function Change_ip ()
  23. {
  24. #判断备份目录是否存在, there are spaces before and after the brackets! The exclamation point in the shell means the opposite.
  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 state of the last operation, which is 0, indicating that the last operation state was correct or successful #
  33. If
  34. [$?-eq 0];then
  35. #read-P Interactive input variable ipaddr, with a space 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. The first three columns are printed by using the. Number as a delimited field
  39. Echo-e "ipaddr= $IPADDR \nnetmask= $NETMASK \ngateway= ' echo $IPADDR |awk-f. ' {print $ '. $ "." $ ". 254" >> $ETHCONF
  40. echo "This IP address change success!"
  41. Else
  42. Echo-n "This $ETHCONF are 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. #定义数组, ${#count [@]} to get the total number of variable values #
  50. a=${#count [@]}
  51. #while条件语句判断, the number is correct, incorrect loop prompt input, you can also use [0-9] to Judge ip#
  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 modify multiple parameters consecutively #
  60. Sed-i-E ' s/^ipaddr/#IPADDR/g '-e ' s/^netmask/#NETMASK/g '-e ' s/^gateway/#GATEWAY/g ' $ETHCONF
  61. #echo-e \ n for continuous append, and wrap line #
  62. Echo-e "ipaddr= $IPADDR \nnetmask= $NETMASK \ngateway= ' echo $IPADDR |awk-f. ' {print $ '. $ "." $ ". 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. #定义hosts函数
  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. #定义network函数
  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. #PS3一般为菜单提示信息 #
  110. ps3="Please select IP or hosts menu":
  111. #select为菜单选择命令 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 judging under a variety of 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

Automatic IP modification of Linux shell scripts

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.