mail_sender.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import smtplib
  6. import subprocess
  7. import time
  8. from configparser import ConfigParser
  9. from email.mime.multipart import MIMEMultipart
  10. from email.mime.text import MIMEText
  11. def sender_mail(BODY):
  12. if not os.path.isdir('report'):
  13. os.mkdir('report')
  14. name_f = str(time.ctime())
  15. open_f = open(str(start_path)+'/report/'+name_f+'.txt', 'w')
  16. open_f.write(BODY)
  17. open_f.close()
  18. HOST = str(cfg_pars.get('smtp', 'server'))
  19. addr_from = str(cfg_pars.get('smtp', 'user'))
  20. password = str(cfg_pars.get('smtp', 'pass'))
  21. EMAILS = str(cfg_pars.get('TO', 'emails'))
  22. SUBJECT = str(cfg_pars.get('SUBJECT', 'subject'))
  23. if 'recips' in cfg_pars:
  24. msg = MIMEMultipart('utf-8')
  25. msg['From'] = addr_from
  26. msg['To'] = ', '.join(EMAILS.split(', '))
  27. msg['Subject'] = SUBJECT
  28. body = MIMEText(BODY)
  29. msg.attach(body)
  30. else:
  31. msg = MIMEMultipart('utf-8')
  32. msg['From'] = addr_from
  33. msg['To'] = EMAILS
  34. msg['Subject'] = SUBJECT
  35. body = MIMEText(BODY)
  36. msg.attach(body)
  37. if 'port' in cfg_pars:
  38. PORT = cfg_pars.get('smtp', 'port')
  39. else:
  40. PORT = 25
  41. if 'ssl' in cfg_pars:
  42. server = smtplib.SMTP_SSL(HOST, PORT)
  43. else:
  44. server = smtplib.SMTP(HOST, PORT)
  45. if 'tls' in cfg_pars:
  46. server.starttls()
  47. if 'debug' in cfg_pars:
  48. server.set_debuglevel(True)
  49. server.login(addr_from, password)
  50. server.send_message(msg)
  51. server.quit()
  52. def run_proc(CMD):
  53. if 'timeout' in cfg_pars:
  54. time=int(cfg_pars.get('timeout', 'seconds'))
  55. else:
  56. time=2000
  57. PROCESS = subprocess.run(CMD,
  58. universal_newlines=True,
  59. shell=True,
  60. stdout=subprocess.PIPE,
  61. stderr=subprocess.PIPE,
  62. timeout=time
  63. )
  64. out = PROCESS.stdout
  65. err = PROCESS.stderr
  66. return out, err
  67. if __name__ == '__main__':
  68. start_path = os.path.dirname(os.path.abspath(__file__))
  69. config_path = os.path.join(start_path, 'config.ini')
  70. if os.path.exists(config_path):
  71. cfg_pars = ConfigParser()
  72. cfg_pars.read(config_path)
  73. else:
  74. print('''
  75. [ERROR]:
  76. Config not found. Need make config.ini''')
  77. sys.exit(1)
  78. CMD = str(' '.join(sys.argv[1::]))
  79. STATE_CMD = run_proc(CMD)
  80. if STATE_CMD[1] != '':
  81. print('''
  82. [WARNING] :
  83. The process ended with an error:
  84. '''+ STATE_CMD[1])
  85. BODY = STATE_CMD[1]
  86. else:
  87. print('''
  88. [MESSAGE] :
  89. '''+STATE_CMD[0])
  90. BODY = STATE_CMD[0]
  91. sender_mail(BODY)
  92. print('[DONE]')