add_ad_users.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2017-2018 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. from pyad import *
  27. import sys
  28. import re
  29. import time
  30. import smtplib
  31. import random
  32. import string
  33. from email.mime.text import MIMEText
  34. AD_DN_BASE = 'cn=Users, dc=ad, dc=ciscolive, dc=network'
  35. DEFAULT_GROUP = 'NOC Users'
  36. AD_DOMAIN = 'ad.ciscolive.network'
  37. if __name__ == '__main__':
  38. if len(sys.argv) != 3:
  39. sys.stderr.write('usage: {} GROUP FILE\n'.format(sys.argv[0]))
  40. sys.exit(1)
  41. #pyad.set_defaults(ldap_server=AD_DC, username=AD_USERNAME, password=AD_PASSWORD, ssl=True)
  42. ou = adcontainer.ADContainer.from_dn(AD_DN_BASE)
  43. fd = open(sys.argv[2])
  44. contents = fd.readlines()
  45. fd.close()
  46. group = sys.argv[1]
  47. MSG = 'Created CLEU account for {}.\r\n\r\n'
  48. MSG += 'Login to the CL-NOC SSID and https://tool.ciscolive.network with the following:\r\n\r\n'
  49. MSG += 'Username: {}\r\n'
  50. MSG += 'Password: {}\r\n'
  51. SUBJECT = 'New CLEU network account'
  52. for line in contents:
  53. line = line.strip()
  54. name, email, username = line.split(',')
  55. try:
  56. ad_user = aduser.ADUser.from_dn('cn={}, {}'.format(
  57. name, AD_DN_BASE))
  58. if ad_user is not None:
  59. sys.stderr.write(
  60. 'Not creating {} as they already exist.\n'.format(username))
  61. continue
  62. except Exception:
  63. pass
  64. password = ''.join(random.choice(string.ascii_uppercase + string.digits +
  65. string.ascii_lowercase + '@!%^#:*') for _ in range(8))
  66. try:
  67. new_user = aduser.ADUser.create(
  68. name, ou, password=password)
  69. except Exception as e:
  70. sys.stderr.write(
  71. "Failed to create user {}: {}\n".format(username, e))
  72. continue
  73. new_user.update_attribute('mail', email)
  74. try:
  75. new_user.update_attribute('sAMAccountName', username)
  76. new_user.update_attribute(
  77. 'userPrincipalName', '{}@{}'.format(username, AD_DOMAIN))
  78. except Exception as e:
  79. new_user.delete()
  80. sys.stderr.write(
  81. 'Error adding user {} (maybe duplicate?) ({})\n'.format(username, e))
  82. continue
  83. def_group = adgroup.ADGroup.from_cn(group)
  84. def_group.add_members([new_user])
  85. print('Added user {}'.format(username))
  86. msg = MIMEText(MSG.format(name, username, password))
  87. msg['Subject'] = SUBJECT
  88. msg['From'] = 'jclarke@cisco.com'
  89. msg['To'] = email
  90. msg['Bcc'] = ','.join(['jclarke@cisco.com'])
  91. sm = smtplib.SMTP('10.100.253.13')
  92. sm.sendmail('jclarke@cisco.com', [
  93. email, 'jclarke@cisco.com'], msg.as_string())
  94. sm.quit()
  95. time.sleep(1)