Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #!/usr/bin/python
  2. import csv
  3. import argparse
  4. import crypt
  5. import sys
  6. import random
  7. import os
  8. import grp
  9. import pwd
  10. import io
  11. import shutil
  12. import re
  13.  
  14. saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  15. arg = 'randomstring'
  16. def copy_file(src, dest):
  17. try:
  18. shutil.copy(src, dest)
  19. # eg. src and dest are the same file
  20. except shutil.Error as e:
  21. print('Error: %s' % e)
  22. # eg. source or destination doesn't exist
  23. except IOError as e:
  24. print('Error: %s' % e.strerror)
  25.  
  26. def salt():
  27. return random.choice(saltchars) + random.choice(saltchars)
  28.  
  29. def hash(plain):
  30. return crypt.crypt(arg, salt())
  31.  
  32. def main():
  33. with io.FileIO("handy_links.html", "w") as file:
  34. file.write("www.google.com\n")
  35. file.write("www.yahoo.com\n")
  36. file.write("www.bing.com\n")
  37. directory = "Handy Links"
  38.  
  39. invalid_chars = re.compile(r"^[^<>/{}[\]~`]*$");
  40.  
  41. parser = argparse.ArgumentParser(description='This program will create user directories plus more')
  42. parser.add_argument("csv", nargs='+', help='Specify a CSV file with users', type=str)
  43. args = parser.parse_args()
  44. #print args.csv[0]
  45. file = args.csv[0]
  46. users = open(file, 'rb')
  47. csv_f = csv.reader(users)
  48. #Define some variables
  49. #The program will assume there are no middle names or initials
  50. for row in csv_f:
  51. random.seed()
  52. full_name = row[0].split()
  53. #print full_name
  54. if not full_name:
  55. continue
  56. first_initial = full_name[0][0]
  57. first_name = full_name[0]
  58. last_name = full_name[1]
  59. username = (first_initial + last_name).lower()
  60. if not username:
  61. continue
  62. elif not last_name:
  63. continue
  64. elif not first_name:
  65. continue
  66. #Generate a password - alphanumeric charachers
  67. #This method is really for checking if the existing passwords are correct, however, generating them #would work as well
  68. password = hash(arg)
  69. try:
  70. pwd.getpwnam(username)
  71. except KeyError:
  72. #only create a user if it does not exist
  73. os.system("useradd -p "+password+" "+username)
  74. #print username
  75. #groups
  76. #check if group are not empty
  77. if row [1:]:
  78. for group in row[1:]:
  79. #print invalid_chars.match(group)
  80. if invalid_chars.match(group):
  81. #print ("group name " + group.strip() + " contains all good chars")
  82. try:
  83. grp.getgrnam(group.strip())
  84. except KeyError:
  85. #create a group if does not exist. why not :)
  86. os.system("groupadd "+group.strip())
  87. #add user to the group
  88. os.system("usermod -a -G "+group.strip()+" "+username)
  89. else:
  90. pass
  91. #print ("group name " + group.strip() + " DOES NOT contain all good char")
  92.  
  93. #create a directory for handy links
  94. if not os.path.exists("/home/"+username+"/"+directory):
  95. os.makedirs("/home/"+username+"/"+directory)
  96. copy_file("handy_links.html", "/home/"+username+"/"+directory+"/handy_links.html")
  97. #script output
  98. print (first_name + " " + last_name + ", " + username + "/" + password)
  99. html_file_name = username + ".html"
  100. #html file created in the same directory where the script runs
  101. web_user_file = open(html_file_name, 'w+')
  102. web_user_file.write(first_name + " " + last_name + ", " + username + "/" + password)
  103.  
  104. try:
  105. main()
  106. except KeyboardInterrupt:
  107. print "Bye"
  108. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement