Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # "go away or i'll replace you with a very small shell script"
- cd /root/scripts/gxp2020ext
- if ! [ -f "oldnames.txt" ]; then
- touch "oldnames.txt"
- fi
- EMAIL_TO="gopher@example.com"
- EMAIL_FROM="noreply@example.com"
- EMAIL_SUBJECT="Company Directory Updated"
- SMTP_SERVER="10.0.0.10"
- getdate() {
- mydate="$(printf '%(%a, %-e %b %Y %H:%M:%S %z)T\n' -1 2>/dev/null)"
- if [ -z "$mydate" ]; then
- mydate="$(date +'%a, %-e %b %Y %H:%M:%S %z')"
- fi
- echo "$mydate"
- }
- email() {
- exec 3<>/dev/tcp/$SMTP_SERVER/25
- read -t30 response <&3
- if [ "${response:0:4}" != "220 " ]; then
- echo "$SMTP_SERVER did not respond with banner in time" >&2
- exit 1
- fi
- echo "HELO elastix" >&3
- read -t30 response <&3
- if [ "${response:0:4}" != "250 " ]; then
- echo "$SMTP_SERVER did not accept HELO" >&2
- exit 1
- fi
- echo "MAIL FROM: <$EMAIL_FROM>" >&3
- read -t30 response <&3
- if [ "${response:0:4}" != "250 " ]; then
- echo "$SMTP_SERVER did not accept MAIL FROM address" >&2
- exit 1
- fi
- echo "RCPT TO: <$EMAIL_TO>" >&3
- read -t30 response <&3
- if [ "${response:0:4}" != "250 " ]; then
- echo "$SMTP_SERVER did not accept RCTP TO" >&2
- exit 1
- fi
- echo "DATA" >&3
- read -t30 response <&3
- if [ "${response:0:4}" != "354 " ]; then
- echo "$SMTP_SERVER did not accept envelope" >&2
- exit 1
- fi
- echo "To: <$EMAIL_TO>" >&3
- echo "From: Elastix server <$EMAIL_FROM>" >&3
- echo "Date: $(getdate)" >&3
- echo "Subject: $EMAIL_SUBJECT" >&3
- #echo "" >&3
- cat >&3
- echo -e "\n." >&3
- read -t30 response <&3
- if [ "${response:0:4}" != "250 " ]; then
- echo "$SMTP_SERVER did not accept message body" >&2
- exit 1
- fi
- echo "QUIT" >&3
- while read -t 10 line <&3
- do
- sleep 0
- #echo "$line"
- done
- exec 3>&-
- }
- ./list_names.sh >names.txt
- diff oldnames.txt names.txt >/dev/null
- if [ "$?" == "0" ]; then
- exit 0
- fi
- if [ -f "template.pdf" ]; then
- rm "template.pdf"
- fi
- ./list_names.sh |awk 'BEGIN { FS = "<" }; {print $1}' |./make_template.sh
- if ! [ -f "template.pdf" ]; then
- echo -e "\nWarning, could not create template.pdf on Elastix server" |email
- exit 1
- fi
- cat names.txt |./provision_gxp2020ext.sh "10.0.0.152"
- # Human friendly description of changes
- changes="$(diff names.txt oldnames.txt |sed 's/^</Added/1' |sed 's/^>/Removed/1' |grep -e "^A\|^R" |grep -ve "^Added $" |grep -ve "^Removed $")"
- cat <<EOF |email
- MIME-Version: 1.0
- Content-Type: multipart/mixed;
- boundary="------------362711A769942E99400F7B04"
- This is a multi-part message in MIME format.
- --------------362711A769942E99400F7B04
- Content-Type: text/plain; charset=utf-8; format=flowed
- Content-Transfer-Encoding: 7bit
- The company phone directory has been updated.
- The lobby phone has been programmed to reflect
- the changes.
- Please print, cut, and replace the name insert
- attached in this email.
- Summary of changes:
- $(echo "$changes")
- --------------362711A769942E99400F7B04
- Content-Type: application/pdf;
- name="gxp2020ext_template.pdf"
- Content-Transfer-Encoding: base64
- Content-Disposition: attachment;
- filename="gxp2020ext_template.pdf"
- $(base64 template.pdf)
- --------------362711A769942E99400F7B04--
- EOF
- mv names.txt oldnames.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement