change.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. #########################################################################
  3. # Script for change important parameters #
  4. # Created by DoIT #
  5. # 1.3 #
  6. #########################################################################
  7. #------------------------------ DEFINITION -----------------------------#
  8. SERIAL="N"
  9. MOD=TEST
  10. #------------------------------- LOCAL IP ------------------------------#
  11. check_nic(){
  12. NICS=$(find /sys/class/net -type l ! -lname '*lo*' -printf '%f\n')
  13. for i in $NICS;
  14. do
  15. CI=$(ifconfig $i | grep -w inet)
  16. if [ ! -z "$CI" ]; then
  17. echo -e "- \e[1;32mActive\e[0m [\e[1;33m$i\e[0m]"
  18. SN=$(ifconfig $i | grep -w ether | sed 's/:/ /g' | awk '{print tolower ($5$6$7)}')
  19. IP=$(ifconfig $i | grep -w inet | awk '{print $2}')
  20. fi
  21. done
  22. if [ -z "$IP" ]; then
  23. echo -e "- \e[1;31mmissing\e[0m IP address"
  24. exit 1
  25. else
  26. echo -e "- \e[1;32mIP-Address\e[0m [\e[1;33m$IP\e[0m]"
  27. fi
  28. OLDURL=$(cat /etc/monit/monitrc | grep "use address" | awk '{print $3}')
  29. }
  30. #--------------------------- UPDATE HOSTNAME ---------------------------#
  31. update_host(){
  32. if [[ "$SERIAL" == "Y" ]]; then
  33. HOST=$(cat /etc/hosts | grep "$MOD-$SN" | awk '{print $2}' | head -1)
  34. HNAME=$(cat /etc/hostname | grep "$MOD-$SN")
  35. if [[ "$MOD-$SN" != "$HOST" ]] || [[ "$MOD-$SN" != "$HNAME" ]];
  36. then
  37. echo -e "- \e[1;33mUpdating\e[0m [\e[1;34mHOST\e[0m]"
  38. chattr -i /etc/hostname
  39. echo "$MOD-$SN" > /etc/hostname
  40. chattr +i /etc/hostname
  41. chattr -i /etc/hosts
  42. echo "127.0.0.1 localhost" > /etc/hosts
  43. echo "127.0.1.1 $MOD-$SN" >> /etc/hosts
  44. chattr +i /etc/hosts
  45. hostname "$MOD-$SN"
  46. fi
  47. else
  48. HOST=$(cat /etc/hosts | grep "$MOD" | awk '{print $2}' | head -1)
  49. HNAME=$(cat /etc/hostname | grep "$MOD")
  50. if [[ "$MOD" != "$HOST" ]] || [[ "$MOD" != "$HNAME" ]];
  51. then
  52. echo -e "- \e[1;33mUpdating\e[0m [\e[1;34mHOST\e[0m]"
  53. chattr -i /etc/hostname
  54. echo "$MOD" > /etc/hostname
  55. chattr +i /etc/hostname
  56. chattr -i /etc/hosts
  57. echo "127.0.0.1 localhost" > /etc/hosts
  58. echo "127.0.1.1 $MOD" >> /etc/hosts
  59. chattr +i /etc/hosts
  60. hostname "$MOD"
  61. fi
  62. fi
  63. }
  64. #----------------------------- UPDATE MONIT ----------------------------#
  65. update_monit(){
  66. MONIT=$(cat /etc/monit/monitrc | grep "check system" | awk '{print $3}')
  67. MONIP=$(cat /etc/monit/monitrc | grep "use address" | awk '{print $3}')
  68. if [[ "$SERIAL" == "Y" ]]; then
  69. if [[ "$MOD-$SN" != "$MONIT" ]] || [[ "$IP" != "$MONIP" ]]; then
  70. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/monit/monitrc\e[0m]"
  71. sed -i "s/check system.*/check system $MOD-$SN/g" /etc/monit/monitrc
  72. sed -i "s/use address.*/use address $IP/g" /etc/monit/monitrc
  73. /etc/init.d/monit restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
  74. fi
  75. else
  76. if [[ "$MOD" != "$MONIT" ]] || [[ "$IP" != "$MONIP" ]]; then
  77. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/monit/monitrc\e[0m]"
  78. sed -i "s/check system.*/check system $MOD/g" /etc/monit/monitrc
  79. sed -i "s/use address.*/use address $IP/g" /etc/monit/monitrc
  80. /etc/init.d/monit restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
  81. fi
  82. fi
  83. }
  84. #----------------------------- UPDATE APC ------------------------------#
  85. update_apc(){
  86. APCD=$(cat /etc/apcupsd/apcupsd.conf | grep UPSNAME | awk '{print $2}')
  87. APCH=$(cat /etc/apcupsd/hosts.conf | grep MONITOR | awk '{print $3}' | sed 's/"//g')
  88. APCI=$(cat /etc/apcupsd/hosts.conf | grep MONITOR | awk '{print $2}' | sed 's/"//g')
  89. if [[ "$SERIAL" == "Y" ]]; then
  90. if [[ "$MOD-$SN" != "$APCD" ]] || [[ "$MOD-$SN" != "$APCH" ]] || [[ "$IP" != "$APCI" ]]; then
  91. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/hosts.conf\e[0m]"
  92. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/apcupsd.conf\e[0m]"
  93. sed -i "s/UPSNAME.*/UPSNAME $MOD-$SN/g" /etc/apcupsd/apcupsd.conf
  94. sed -i "s/MONITOR.*/MONITOR $IP \"$MOD-$SN\"/g" /etc/apcupsd/hosts.conf
  95. /etc/init.d/apcupsd restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
  96. fi
  97. else
  98. if [[ "$MOD" != "$APCD" ]] || [[ "$MOD" != "$APCH" ]] || [[ "$IP" != "$APCI" ]]; then
  99. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/hosts.conf\e[0m]"
  100. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/apcupsd.conf\e[0m]"
  101. sed -i "s/UPSNAME.*/UPSNAME $MOD/g" /etc/apcupsd/apcupsd.conf
  102. sed -i "s/MONITOR.*/MONITOR $IP \"$MOD\"/g" /etc/apcupsd/hosts.conf
  103. /etc/init.d/apcupsd restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
  104. fi
  105. fi
  106. }
  107. #--------------------------- UPDATE POSTFIX ----------------------------#
  108. update_postfix(){
  109. POSTFIX=$(cat /etc/postfix/main.cf | grep -w "^myhostname" | awk '{print $3}')
  110. if [[ "$SERIAL" == "Y" ]]; then
  111. if [[ "$MOD-$SN" != "$POSTFIX" ]]; then
  112. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/postfix/main.cf\e[0m]"
  113. sed -i "s/myhostname =.*/myhostname = $MOD-$SN/g" /etc/postfix/main.cf
  114. sed -i "s/mydestination = \$myhostname,.*/mydestination = \$myhostname, $MOD-$SN, localhost/g" /etc/postfix/main.cf
  115. newaliases
  116. /etc/init.d/postfix restart > /dev/null 2>&1 || systemctl restart postfix > /dev/null 2>&1
  117. fi
  118. else
  119. if [[ "$MOD" != "$POSTFIX" ]]; then
  120. echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/postfix/main.cf\e[0m]"
  121. sed -i "s/myhostname =.*/myhostname = $MOD/g" /etc/postfix/main.cf
  122. sed -i "s/mydestination = \$myhostname,.*/mydestination = \$myhostname, $MOD, localhost/g" /etc/postfix/main.cf
  123. newaliases
  124. /etc/init.d/postfix restart > /dev/null 2>&1 || systemctl restart postfix > /dev/null 2>&1
  125. fi
  126. fi
  127. }
  128. #-------------------------- UPDATE WEB SOURCE --------------------------#
  129. update_web(){
  130. OLDURL=$(cat /var/www/index.html | grep http | awk '{print $4}' | sed 's/\/\// /g' | sed 's/\// /g' | sed 's/:/ /g' | awk '{print $2}' | sort | uniq | grep [0-9])
  131. LST=$(find /var/www/ -name index.htm*)
  132. if [[ "$OLDURL" != "$IP" ]]; then
  133. echo -e "- \e[1;33mUpdating\e[0m \e[1;34mIP\e[0m [\e[1;35m$IP\e[0m]"
  134. for w in $LST;
  135. do
  136. sed -i "s/$OLDURL/$IP/g" $w
  137. done
  138. fi
  139. for w in $LST;
  140. do
  141. OLDHST=$(cat $w | grep -i TITLE | sed 's/>/ /g' | sed 's/</ /g' | awk '{print $2}')
  142. if [[ "$SERIAL" == "Y" ]]; then
  143. if [[ "$OLDHST" != "$MOD-$SN" ]]; then
  144. echo -e "- \e[1;33mUpdating\e[0m \e[1;34mHOST\e[0m [\e[1;35m$w\e[0m]"
  145. sed -i "s/<TITLE>.*/<TITLE>$MOD-$SN<\/TITLE>/g" $w
  146. sed -i "s/<title>.*/<title>$MOD-$SN<\/title>/g" $w
  147. # sed -i "s/on server.*/on server [<b>$MOD-$SN<\/b>]<\/font><\/p>/g" $w
  148. sed -i "/on server/s/\[[^]]*\]/[$MOD-$SN]/g" $w
  149. fi
  150. else
  151. if [[ "$OLDHST" != "$MOD" ]]; then
  152. echo -e "- \e[1;33mUpdating\e[0m \e[1;34mHOST\e[0m [\e[1;35m$w\e[0m]"
  153. sed -i "s/<TITLE>.*/<TITLE>$MOD<\/TITLE>/g" $w
  154. sed -i "s/<title>.*/<title>$MOD<\/title>/g" $w
  155. # sed -i "s/on server.*/on server [<b>$MOD<\/b>]<\/font><\/p>/g" $w
  156. sed -i "/on server/s/\[[^]]*\]/[$SRV]/g" $w
  157. fi
  158. fi
  159. done
  160. }
  161. #--------------------------------- CORE --------------------------------#
  162. clear
  163. check_nic
  164. update_host
  165. test -f /etc/monit/monitrc && update_monit
  166. test -f /etc/postfix/main.cf && update_postfix
  167. test -f /etc/apcupsd/apcupsd.conf && update_apc
  168. test -d /var/www/ && update_web