add_ad_users.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(name, AD_DN_BASE))
  57. if ad_user is not None:
  58. sys.stderr.write("Not creating {} as they already exist.\n".format(username))
  59. continue
  60. except Exception:
  61. pass
  62. password = "".join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase + "@!%^#:*") for _ in range(8))
  63. try:
  64. new_user = aduser.ADUser.create(name, ou, password=password)
  65. except Exception as e:
  66. sys.stderr.write("Failed to create user {}: {}\n".format(username, e))
  67. continue
  68. new_user.update_attribute("mail", email)
  69. try:
  70. new_user.update_attribute("sAMAccountName", username)
  71. new_user.update_attribute("userPrincipalName", "{}@{}".format(username, AD_DOMAIN))
  72. except Exception as e:
  73. new_user.delete()
  74. sys.stderr.write("Error adding user {} (maybe duplicate?) ({})\n".format(username, e))
  75. continue
  76. def_group = adgroup.ADGroup.from_cn(group)
  77. def_group.add_members([new_user])
  78. print("Added user {}".format(username))
  79. msg = MIMEText(MSG.format(name, username, password))
  80. msg["Subject"] = SUBJECT
  81. msg["From"] = "jclarke@cisco.com"
  82. msg["To"] = email
  83. msg["Bcc"] = ",".join(["jclarke@cisco.com"])
  84. sm = smtplib.SMTP("10.100.253.13")
  85. sm.sendmail("jclarke@cisco.com", [email, "jclarke@cisco.com"], msg.as_string())
  86. sm.quit()
  87. time.sleep(1)