ghettoVCB mail senza netcat

Sto provando lo script ghettoVCB.sh per effettuare i backup su ESX e ESXi. In breve, questo script necessiterebbe di netcat per inviare la mail all’admin sul risultato dei backup. Ma l’invio della mail fallisce se inviata verso server smtp che effettuano il controllo sul pipelining a fini antispam (ad esempio Postfix configurato con usuali regole antispam rifiuta il messaggio con l’errore “Improper use of SMTP command pipelining”). In più, a me non piace l’idea di installare netcat su un ESX, se posso evitarlo.
Questa patch risolve entrambi i problemi. Testato su ESX 3.5. Non può funzionare su ESXi.

--- ghettoVCB.sh.orig   2010-12-20 23:38:09.000000000 +0100
+++ ghettoVCB.sh        2010-12-21 14:40:02.000000000 +0100
@@ -79,7 +79,7 @@
 # Email debug 1=yes, 0=no
 EMAIL_DEBUG=0
 
-# Email log 1=yes, 0=no 
+# Email log 1=yes via netcat (if available), 2=yes via bash' builtin, 0=no
 EMAIL_LOG=0
 
 # Email SMTP server
@@ -162,7 +162,7 @@
                                echo -e "${TIME} -- ${LOG_TYPE}: ${MSG}" >> "${LOG_OUTPUT}"
                fi
 
-               if [ "${EMAIL_LOG}" -eq 1 ]; then
+               if [ "${EMAIL_LOG}" -ne 0 ]; then
                        echo -ne "${TIME} -- ${LOG_TYPE}: ${MSG}\r\n" >> "${EMAIL_LOG_OUTPUT}"
                fi
        fi
@@ -249,6 +249,10 @@
                echo -ne "From: ${EMAIL_FROM}\r\n" >> "${EMAIL_LOG_OUTPUT}"
                echo -ne "To: ${EMAIL_TO}\r\n" >> "${EMAIL_LOG_OUTPUT}"
                echo -ne "Subject: ghettoVCB - $(date)\r\n" >> "${EMAIL_LOG_OUTPUT}" 
+       elif [[ "${EMAIL_LOG}" -eq 2 ]]; then
+               echo -ne "From: ${EMAIL_FROM}\r\n" >> "${EMAIL_LOG_OUTPUT}"
+               echo -ne "To: ${EMAIL_TO}\r\n" >> "${EMAIL_LOG_OUTPUT}"
+               echo -ne "Subject: ghettoVCB - $(date)\r\n" >> "${EMAIL_LOG_OUTPUT}" 
         else 
                EMAIL_LOG=0
        fi
@@ -429,7 +433,7 @@
         logger "info" "CONFIG - VM_SNAPSHOT_QUIESCE = ${VM_SNAPSHOT_QUIESCE}"
         logger "info" "CONFIG - VMDK_FILES_TO_BACKUP = ${VMDK_FILES_TO_BACKUP}"
        logger "info" "CONFIG - EMAIL_LOG = ${EMAIL_LOG}"
-       if [ "${EMAIL_LOG}" -eq 1 ]; then
+       if [ "${EMAIL_LOG}" -ne 0 ]; then
                logger "info" "CONFIG - EMAIL_DEBUG = ${EMAIL_DEBUG}"
                logger "info" "CONFIG - EMAIL_SERVER = ${EMAIL_SERVER}"
                logger "info" "CONFIG - EMAIL_SERVER_PORT = ${EMAIL_SERVER_PORT}"
@@ -886,12 +890,70 @@
                if [ $? -eq 1 ]; then
                        logger "info" "ERROR: Failed to email log output to ${EMAIL_SERVER}:${EMAIL_SERVER_PORT} to ${EMAIL_TO}\n"
                fi
-
-               if [ "${EMAIL_DEBUG}" -eq 1 ]; then
-                       logger "info" "Email log output will not be deleted and is stored in ${EMAIL_LOG_OUTPUT}\n"
+       elif [ "${EMAIL_LOG}" -eq 2 ]; then
+
+               exec 3<>/dev/tcp/${EMAIL_SERVER}/${EMAIL_SERVER_PORT}
+               if [ $? -ne 0 ] ; then
+                       ERRSM="Failed to connect to ${EMAIL_SERVER}:${EMAIL_SERVER_PORT}"
                else
-                       rm -rf "${EMAIL_LOG_OUTPUT}"
+                       ERRSM=
+               fi
+
+               if [ ! "$ERRSM" ] ; then
+                       # Read banner
+                       read -t 5 resp <&3
+                       [[ "$resp" == 2* ]] || ERRSM="Failed reading banner from ${EMAIL_SERVER}:${EMAIL_SERVER_PORT}"
+               fi
+
+               if [ ! "$ERRSM" ] ; then
+                       # send command
+                       echo -ne "HELO $(hostname -s)\r\n" >&3
+                       # Read answer
+                       read -t 5 resp <&3
+                       [[ "$resp" == 2* ]] || ERRSM="Failed sending HELO command to ${EMAIL_SERVER}:${EMAIL_SERVER_PORT}"
+               fi
+
+               if [ ! "$ERRSM" ] ; then
+                       echo -ne "MAIL FROM: <${EMAIL_FROM}>\r\n" >&3
+                       read -t 5 resp <&3
+                       [[ "$resp" == 2* ]] || ERRSM="Failed sending MAIL FROM command to ${EMAIL_SERVER}:${EMAIL_SERVER_PORT}"
+               fi
+
+               if [ ! "$ERRSM" ] ; then
+                       echo -ne "RCPT TO: <${EMAIL_TO}>\r\n" >&3
+                       read -t 5 resp <&3
+                       [[ "$resp" == 2* ]] || ERRSM="Failed sending RCPT TO command to ${EMAIL_SERVER}:${EMAIL_SERVER_PORT}"
+               fi
+
+               if [ ! "$ERRSM" ] ; then
+                       echo -ne "DATA\r\n" >&3
+                       read -t 5 resp <&3
+                       [[ "$resp" == 3* ]] || ERRSM="Failed sending DATA command to ${EMAIL_SERVER}:${EMAIL_SERVER_PORT}"
+               fi
+
+               if [ ! "$ERRSM" ] ; then
+                       cat "${EMAIL_LOG_OUTPUT}" >&3
+                       echo -en "\r\n.\r\n" >&3
+                       read -t 5 resp <&3
+                       [[ "$resp" == 2* ]] || ERRSM="Failed sending the message body to ${EMAIL_SERVER}:${EMAIL_SERVER_PORT}"
+               fi
+
+               if [ ! "$ERRSM" ] ; then
+                       echo -ne "QUIT\r\n" >&3
+                       cat <&3 > /dev/null
                fi
+
+               if [ "$ERRSM" ] ; then
+                       logger "info" "ERROR: ${ERRSM} while sending to ${EMAIL_TO}\n"
+               fi
+       else
+               return
+       fi
+
+       if [ "${EMAIL_DEBUG}" -eq 1 ]; then
+               logger "info" "Email log output will not be deleted and is stored in ${EMAIL_LOG_OUTPUT}\n"
+       else
+               rm -rf "${EMAIL_LOG_OUTPUT}"
        fi
 }

Comments are closed.