Browse Source

Script for change hostname and address in all configuration files

pmacko 1 month ago
parent
commit
ecb73adcae
1 changed files with 101 additions and 0 deletions
  1. 101 0
      change.sh

+ 101 - 0
change.sh

@@ -0,0 +1,101 @@
+#!/bin/bash
+#########################################################################
+#                Script for change important parameters                 #
+#                           Created by PM-DoIT                          #
+#                                  0.7                                  #
+#########################################################################
+
+#------------------------------ DEFINITION -----------------------------#
+MOD=BASERPI
+
+#------------------------------- LOCAL IP ------------------------------#
+check_nic(){
+NICS=$(find /sys/class/net -type l -not -lname '*virtual*' -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}')
+fi
+done
+if [ -z "$IP" ]; then
+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]"
+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"
+fi
+}
+
+#----------------------------- UPDATE MONIT ----------------------------#
+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
+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
+fi
+}
+
+#-------------------------- UPDATE WEB SOURCE --------------------------#
+update_web(){
+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]"
+for w in $LST;
+do
+sed -i "s/$OLDURL/$IP/g" $w
+done
+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
+fi
+done
+}
+
+#--------------------------------- CORE --------------------------------#
+clear
+check_nic
+update_host
+update_monit
+update_postfix
+update_web