admin scripts
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

95 linhas
3.3 KiB

  1. #!/usr/bin/python
  2. """
  3. -maK
  4. This is a script to send a mail to a group
  5. defined by an ldap search
  6. or simply send a singular mail
  7. use ./groupmail -h
  8. """
  9. import smtplib, string, argparse, os
  10. def sendmail(frm, to, subject, body, cc):
  11. if(cc == None):
  12. cc = ''
  13. msg = string.join((
  14. 'From: %s' % frm,
  15. 'To: %s' % to,
  16. 'CC: %s' % cc,
  17. 'Subject: %s' % subject,
  18. '',
  19. '%s' % body), "\r\n")
  20. try:
  21. smtp = smtplib.SMTP('mail.redbrick.dcu.ie')
  22. smtp.sendmail(frm, to, msg)
  23. smtp.quit()
  24. except:
  25. print 'Error sending mail to ' + to
  26. def mailgroup(ldif, frm, subject, body, cc, prnt):
  27. tomail = string.split(ldif, 'dn: uid=')
  28. cn = ''
  29. altmail = ''
  30. uid = ''
  31. for u in range(1,len(tomail)):
  32. credentials = string.split(tomail[u], '\n')
  33. for i in credentials:
  34. if(i.startswith('uid: ')):
  35. uid = i.split()[1]
  36. if(i.startswith('cn: ')):
  37. cn = i.split('cn: ')[1]
  38. if(i.startswith('altmail: ')):
  39. altmail = i.split()[1]
  40. if(prnt):
  41. print uid + ' - ' + cn + ' : ' + altmail
  42. else:
  43. #Send mail to each
  44. print 'Send mail to '+cn+' using '+altmail+' (y|n)?'
  45. send = raw_input('default(y): ')
  46. if((send == 'n') or (send == 'N')):
  47. print 'Mail not sent to '+cn+' using '+altmail+'\n'
  48. else:
  49. sendmail(frm, altmail.strip(), subject, body, cc)
  50. print 'Mail sent to '+cn+' using '+altmail+'\n'
  51. def main():
  52. parser = argparse.ArgumentParser(description='Used to mail an ldap group/groups')
  53. parser.add_argument('-g',dest='GROUPS', type=str, help='Specify groups or singular group. (eg. club or club,society,founders,redbrick)')
  54. parser.add_argument('-p',dest='PRINT', action='store_true', help='Only print details - Do not send')
  55. parser.add_argument('-f',dest='FROM', type=str, help='Who the mail is from.')
  56. parser.add_argument('-cc',dest='CC', type=str, help='cc all mails to.')
  57. parser.add_argument('-m',dest='MSG', type=str, help='Location of Message to be sent (text file etc)')
  58. parser.add_argument('-t',dest='TO', type=str, help='Who singular mail is To.')
  59. parser.add_argument('-s',dest='SUBJECT', type=str, help='Subject of mail.')
  60. a = parser.parse_args()
  61. if a.GROUPS != None:
  62. if ',' in a.GROUPS:
  63. searchParams = string.split(a.GROUPS, ',')
  64. else:
  65. searchParams = a.GROUPS
  66. ldapsearch = 'ldapsearch -D cn=root,ou=ldap,o=redbrick -y /etc/ldap.secret -xLLL '
  67. if isinstance(searchParams, str):
  68. ldapsearch += '"(objectClass='+searchParams+')" uid cn altmail'
  69. else:
  70. ldapsearch += '"(|'
  71. for i in searchParams:
  72. ldapsearch += '(objectClass='+ i +')'
  73. ldapsearch += ')" uid cn altmail'
  74. ldif = os.popen(ldapsearch).read()
  75. mailgroup(ldif, '','','','',a.PRINT)
  76. exit()
  77. if a.MSG != None:
  78. with open(a.MSG, 'r') as content:
  79. a.MSG = content.read()
  80. if a.GROUPS == None and a.FROM != None and a.TO != None and a.SUBJECT != None and a.MSG != None and a.PRINT == False:
  81. sendmail(a.FROM, a.TO, a.SUBJECT, a.MSG, a.CC)
  82. if __name__ == '__main__':
  83. main()