Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. # "go away or i'll replace you with a very small shell script"
  5.  
  6. cd /root/scripts/gxp2020ext
  7.  
  8. if ! [ -f "oldnames.txt" ]; then
  9.         touch "oldnames.txt"
  10. fi
  11.  
  12. EMAIL_TO="gopher@example.com"
  13. EMAIL_FROM="noreply@example.com"
  14. EMAIL_SUBJECT="Company Directory Updated"
  15. SMTP_SERVER="10.0.0.10"
  16.  
  17. getdate() {
  18.         mydate="$(printf '%(%a, %-e %b %Y %H:%M:%S %z)T\n' -1 2>/dev/null)"
  19.         if [ -z "$mydate" ]; then
  20.                 mydate="$(date +'%a, %-e %b %Y %H:%M:%S %z')"
  21.         fi
  22.         echo "$mydate"
  23. }
  24. email() {
  25.         exec 3<>/dev/tcp/$SMTP_SERVER/25
  26.  
  27.         read -t30 response <&3
  28.     if [ "${response:0:4}" != "220 " ]; then
  29.         echo "$SMTP_SERVER did not respond with banner in time" >&2
  30.         exit 1
  31.     fi
  32.     echo "HELO elastix" >&3
  33.         read -t30 response <&3
  34.     if [ "${response:0:4}" != "250 " ]; then
  35.         echo "$SMTP_SERVER did not accept HELO" >&2
  36.         exit 1
  37.     fi
  38.     echo "MAIL FROM: <$EMAIL_FROM>" >&3
  39.         read -t30 response <&3
  40.     if [ "${response:0:4}" != "250 " ]; then
  41.         echo "$SMTP_SERVER did not accept MAIL FROM address" >&2
  42.         exit 1
  43.     fi
  44.     echo "RCPT TO: <$EMAIL_TO>" >&3
  45.         read -t30 response <&3
  46.     if [ "${response:0:4}" != "250 " ]; then
  47.         echo "$SMTP_SERVER did not accept RCTP TO" >&2
  48.         exit 1
  49.     fi
  50.     echo "DATA" >&3
  51.         read -t30 response <&3
  52.     if [ "${response:0:4}" != "354 " ]; then
  53.         echo "$SMTP_SERVER did not accept envelope" >&2
  54.         exit 1
  55.     fi
  56.     echo "To: <$EMAIL_TO>" >&3
  57.         echo "From: Elastix server <$EMAIL_FROM>" >&3
  58.     echo "Date: $(getdate)" >&3
  59.         echo "Subject: $EMAIL_SUBJECT" >&3
  60.         #echo "" >&3
  61.     cat >&3
  62.     echo -e "\n." >&3
  63.     read -t30 response <&3
  64.     if [ "${response:0:4}" != "250 " ]; then
  65.         echo "$SMTP_SERVER did not accept message body" >&2
  66.         exit 1
  67.     fi
  68.         echo "QUIT" >&3
  69.         while read -t 10 line <&3
  70.     do
  71.         sleep 0
  72.         #echo "$line"
  73.     done
  74.     exec 3>&-
  75. }
  76.  
  77.  
  78. ./list_names.sh >names.txt
  79.  
  80. diff oldnames.txt names.txt >/dev/null
  81. if [ "$?" ==  "0" ]; then
  82.         exit 0
  83. fi
  84.  
  85. if [ -f "template.pdf" ]; then
  86.         rm "template.pdf"
  87. fi
  88.  
  89. ./list_names.sh |awk 'BEGIN { FS = "<" }; {print $1}' |./make_template.sh
  90.  
  91. if ! [ -f "template.pdf" ]; then
  92.         echo -e "\nWarning, could not create template.pdf on Elastix server" |email
  93.         exit 1
  94. fi
  95.  
  96. cat names.txt |./provision_gxp2020ext.sh "10.0.0.152"
  97.  
  98. # Human friendly description of changes
  99. changes="$(diff names.txt oldnames.txt |sed 's/^</Added/1' |sed 's/^>/Removed/1' |grep -e "^A\|^R" |grep -ve "^Added $" |grep -ve "^Removed $")"
  100.  
  101. cat <<EOF |email
  102. MIME-Version: 1.0
  103. Content-Type: multipart/mixed;
  104.  boundary="------------362711A769942E99400F7B04"
  105.  
  106. This is a multi-part message in MIME format.
  107. --------------362711A769942E99400F7B04
  108. Content-Type: text/plain; charset=utf-8; format=flowed
  109. Content-Transfer-Encoding: 7bit
  110.  
  111. The company phone directory has been updated.
  112.  
  113. The lobby phone has been programmed to reflect
  114. the changes.
  115.  
  116. Please print, cut, and replace the name insert
  117. attached in this email.
  118.  
  119. Summary of changes:
  120.  
  121. $(echo "$changes")
  122.  
  123.  
  124. --------------362711A769942E99400F7B04
  125. Content-Type: application/pdf;
  126.  name="gxp2020ext_template.pdf"
  127. Content-Transfer-Encoding: base64
  128. Content-Disposition: attachment;
  129.  filename="gxp2020ext_template.pdf"
  130.  
  131. $(base64 template.pdf)
  132. --------------362711A769942E99400F7B04--
  133. EOF
  134.  
  135. mv names.txt oldnames.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement