Преглед на файлове

Extended change section for new web structure

pmacko преди 2 дни
родител
ревизия
1e7509737b
променени са 1 файла, в които са добавени 117 реда и са изтрити 41 реда
  1. 117 41
      change.sh

+ 117 - 41
change.sh

@@ -1,48 +1,68 @@
 #!/bin/bash
 #########################################################################
 #                Script for change important parameters                 #
-#                           Created by PM-DoIT                          #
-#                                  0.7                                  #
+#                            Created by DoIT                            #
+#                                  1.3                                  #
 #########################################################################
 
 #------------------------------ DEFINITION -----------------------------#
-MOD=BASERPI
+SERIAL="N"
+MOD=TEST
 
 #------------------------------- LOCAL IP ------------------------------#
 check_nic(){
-NICS=$(find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n')
+NICS=$(find /sys/class/net -type l ! -lname '*lo*' -printf '%f\n')
 for i in $NICS;
 do
 CI=$(ifconfig $i | grep -w inet)
 if [ ! -z "$CI" ]; then
-echo -e "- \e[1;32mActive\e[0m [\e[1;33m$i\e[0m]"
-SN=$(ifconfig $i | grep -w ether | sed 's/:/ /g' | awk '{print tolower ($5$6$7)}')
-IP=$(ifconfig $i | grep -w inet | awk '{print $2}')
+        echo -e "- \e[1;32mActive\e[0m [\e[1;33m$i\e[0m]"
+        SN=$(ifconfig $i | grep -w ether | sed 's/:/ /g' | awk '{print tolower ($5$6$7)}')
+        IP=$(ifconfig $i | grep -w inet | awk '{print $2}')
 fi
 done
 if [ -z "$IP" ]; then
-echo -e "- \e[1;31mmissing\e[0m IP address"
+        echo -e "- \e[1;31mmissing\e[0m IP address"
 exit 1
 else
-echo -e "- \e[1;32mIP-Address\e[0m [\e[1;33m$IP\e[0m]"
+        echo -e "- \e[1;32mIP-Address\e[0m [\e[1;33m$IP\e[0m]"
 fi
 OLDURL=$(cat /etc/monit/monitrc | grep "use address" | awk '{print $3}')
-if [ -z "$OLDURL" ]; then
-OLDURL=$(cat $WEB | grep http | awk '{print $4}' | sed 's/\/\// /g' | sed 's/\// /g' | sed 's/:/ /g' | awk '{print $2}' | sort | uniq | grep [0-9])
-fi
 }
 
 #--------------------------- UPDATE HOSTNAME ---------------------------#
 update_host(){
-if ! grep -qs "$MOD-$SN" /etc/hosts;
-then
-echo -e "- \e[1;33mUpdating\e[0m [\e[1;34mHOST\e[0m]"
-echo "$MOD-$SN" > /etc/hostname
-chattr -i /etc/hosts
-echo "127.0.0.1 localhost" > /etc/hosts
-echo "127.0.1.1 $MOD-$SN" >> /etc/hosts
-chattr +i /etc/hosts
-hostname "$MOD-$SN"
+if [[ "$SERIAL" == "Y" ]]; then
+        HOST=$(cat /etc/hosts | grep "$MOD-$SN" | awk '{print $2}' | head -1)
+        HNAME=$(cat /etc/hostname | grep "$MOD-$SN")
+
+        if [[ "$MOD-$SN" != "$HOST" ]] || [[ "$MOD-$SN" != "$HNAME" ]];
+        then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;34mHOST\e[0m]"
+                chattr -i /etc/hostname
+                echo "$MOD-$SN" > /etc/hostname
+                chattr +i /etc/hostname
+                chattr -i /etc/hosts
+                echo "127.0.0.1 localhost" > /etc/hosts
+                echo "127.0.1.1 $MOD-$SN" >> /etc/hosts
+                chattr +i /etc/hosts
+                hostname "$MOD-$SN"
+        fi
+else
+        HOST=$(cat /etc/hosts | grep "$MOD" | awk '{print $2}' | head -1)
+        HNAME=$(cat /etc/hostname | grep "$MOD")
+        if [[ "$MOD" != "$HOST" ]] || [[ "$MOD" != "$HNAME" ]];
+        then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;34mHOST\e[0m]"
+                chattr -i /etc/hostname
+                echo "$MOD" > /etc/hostname
+                chattr +i /etc/hostname
+                chattr -i /etc/hosts
+                echo "127.0.0.1 localhost" > /etc/hosts
+                echo "127.0.1.1 $MOD" >> /etc/hosts
+                chattr +i /etc/hosts
+                hostname "$MOD"
+        fi
 fi
 }
 
@@ -50,31 +70,75 @@ fi
 update_monit(){
 MONIT=$(cat /etc/monit/monitrc | grep "check system" | awk '{print $3}')
 MONIP=$(cat /etc/monit/monitrc | grep "use address" | awk '{print $3}')
-if [[ "$MOD-$SN" != "$MONIT" ]] || [[ "$IP" != "$MONIP" ]]; then
-echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/monit/monitrc\e[0m]"
-sed -i "s/check system.*/check system $MOD-$SN/g" /etc/monit/monitrc
-sed -i "s/use address.*/use address $IP/g" /etc/monit/monitrc
-/etc/init.d/monit restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
+if [[ "$SERIAL" == "Y" ]]; then
+        if [[ "$MOD-$SN" != "$MONIT" ]] || [[ "$IP" != "$MONIP" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/monit/monitrc\e[0m]"
+                sed -i "s/check system.*/check system $MOD-$SN/g" /etc/monit/monitrc
+                sed -i "s/use address.*/use address $IP/g" /etc/monit/monitrc
+                /etc/init.d/monit restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
+        fi
+else
+        if [[ "$MOD" != "$MONIT" ]] || [[ "$IP" != "$MONIP" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/monit/monitrc\e[0m]"
+                sed -i "s/check system.*/check system $MOD/g" /etc/monit/monitrc
+                sed -i "s/use address.*/use address $IP/g" /etc/monit/monitrc
+                /etc/init.d/monit restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
+        fi
+fi
+}
+
+#----------------------------- UPDATE APC ------------------------------#
+update_apc(){
+APCD=$(cat /etc/apcupsd/apcupsd.conf | grep UPSNAME | awk '{print $2}')
+APCH=$(cat /etc/apcupsd/hosts.conf | grep MONITOR | awk '{print $3}' | sed 's/"//g')
+APCI=$(cat /etc/apcupsd/hosts.conf | grep MONITOR | awk '{print $2}' | sed 's/"//g')
+if [[ "$SERIAL" == "Y" ]]; then
+        if [[ "$MOD-$SN" != "$APCD" ]] || [[ "$MOD-$SN" != "$APCH" ]] || [[ "$IP" != "$APCI" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/hosts.conf\e[0m]"
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/apcupsd.conf\e[0m]"
+                sed -i "s/UPSNAME.*/UPSNAME $MOD-$SN/g" /etc/apcupsd/apcupsd.conf
+                sed -i "s/MONITOR.*/MONITOR $IP \"$MOD-$SN\"/g" /etc/apcupsd/hosts.conf
+                /etc/init.d/apcupsd restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
+        fi
+else
+        if [[ "$MOD" != "$APCD" ]] || [[ "$MOD" != "$APCH" ]] || [[ "$IP" != "$APCI" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/hosts.conf\e[0m]"
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/apcupsd/apcupsd.conf\e[0m]"
+                sed -i "s/UPSNAME.*/UPSNAME $MOD/g" /etc/apcupsd/apcupsd.conf
+                sed -i "s/MONITOR.*/MONITOR $IP \"$MOD\"/g" /etc/apcupsd/hosts.conf
+                /etc/init.d/apcupsd restart > /dev/null 2>&1 || systemctl restart monit > /dev/null 2>&1
+        fi
 fi
 }
 
 #--------------------------- UPDATE POSTFIX ----------------------------#
 update_postfix(){
 POSTFIX=$(cat /etc/postfix/main.cf | grep -w "^myhostname" | awk '{print $3}')
-if [[ "$MOD-$SN" != "$POSTFIX" ]]; then
-echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/postfix/main.cf\e[0m]"
-sed -i "s/myhostname =.*/myhostname = $MOD-$SN/g" /etc/postfix/main.cf
-sed -i "s/mydestination = \$myhostname,.*/mydestination = \$myhostname, $MOD-$SN, localhost/g" /etc/postfix/main.cf
-newaliases
-/etc/init.d/postfix restart > /dev/null 2>&1 || systemctl restart postfix > /dev/null 2>&1
+if [[ "$SERIAL" == "Y" ]]; then
+        if [[ "$MOD-$SN" != "$POSTFIX" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/postfix/main.cf\e[0m]"
+                sed -i "s/myhostname =.*/myhostname = $MOD-$SN/g" /etc/postfix/main.cf
+                sed -i "s/mydestination = \$myhostname,.*/mydestination = \$myhostname, $MOD-$SN, localhost/g" /etc/postfix/main.cf
+                newaliases
+                /etc/init.d/postfix restart > /dev/null 2>&1 || systemctl restart postfix > /dev/null 2>&1
+        fi
+else
+        if [[ "$MOD" != "$POSTFIX" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m [\e[1;35m/etc/postfix/main.cf\e[0m]"
+                sed -i "s/myhostname =.*/myhostname = $MOD/g" /etc/postfix/main.cf
+                sed -i "s/mydestination = \$myhostname,.*/mydestination = \$myhostname, $MOD, localhost/g" /etc/postfix/main.cf
+                newaliases
+                /etc/init.d/postfix restart > /dev/null 2>&1 || systemctl restart postfix > /dev/null 2>&1
+        fi
 fi
 }
 
 #-------------------------- UPDATE WEB SOURCE --------------------------#
 update_web(){
+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])
 LST=$(find /var/www/ -name index.htm*)
 if [[ "$OLDURL" != "$IP" ]]; then
-echo -e "- \e[1;33mUpdating\e[0m \e[1;34mIP\e[0m [\e[1;35m$w\e[0m]"
+echo -e "- \e[1;33mUpdating\e[0m \e[1;34mIP\e[0m [\e[1;35m$IP\e[0m]"
 for w in $LST;
 do
 sed -i "s/$OLDURL/$IP/g" $w
@@ -83,11 +147,22 @@ fi
 for w in $LST;
 do
 OLDHST=$(cat $w | grep -i TITLE | sed 's/>/ /g' | sed 's/</ /g' | awk '{print $2}')
-if [[ "$OLDHST" != "$MOD-$SN" ]]; then
-echo -e "- \e[1;33mUpdating\e[0m \e[1;34mHOST\e[0m [\e[1;35m$w\e[0m]"
-sed -i "s/<TITLE>.*/<TITLE>$MOD-$SN<\/TITLE>/g" $w
-sed -i "s/<title>.*/<title>$MOD-$SN<\/title>/g" $w
-sed -i "s/on server.*/on server $MOD-$SN<\/font><\/p>/g" $w
+if [[ "$SERIAL" == "Y" ]]; then
+        if [[ "$OLDHST" != "$MOD-$SN" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m \e[1;34mHOST\e[0m [\e[1;35m$w\e[0m]"
+                sed -i "s/<TITLE>.*/<TITLE>$MOD-$SN<\/TITLE>/g" $w
+                sed -i "s/<title>.*/<title>$MOD-$SN<\/title>/g" $w
+#               sed -i "s/on server.*/on server [<b>$MOD-$SN<\/b>]<\/font><\/p>/g" $w
+                sed -i "/on server/s/\[[^]]*\]/[$MOD-$SN]/g" $w
+        fi
+else
+        if [[ "$OLDHST" != "$MOD" ]]; then
+                echo -e "- \e[1;33mUpdating\e[0m \e[1;34mHOST\e[0m [\e[1;35m$w\e[0m]"
+                sed -i "s/<TITLE>.*/<TITLE>$MOD<\/TITLE>/g" $w
+                sed -i "s/<title>.*/<title>$MOD<\/title>/g" $w
+#               sed -i "s/on server.*/on server [<b>$MOD<\/b>]<\/font><\/p>/g" $w
+                sed -i "/on server/s/\[[^]]*\]/[$SRV]/g" $w
+        fi
 fi
 done
 }
@@ -96,6 +171,7 @@ done
 clear
 check_nic
 update_host
-update_monit
-update_postfix
-update_web
+test -f /etc/monit/monitrc && update_monit
+test -f /etc/postfix/main.cf && update_postfix
+test -f /etc/apcupsd/apcupsd.conf && update_apc
+test -d /var/www/ && update_web