Advertisement
FedeCuci

Untitled

Apr 21st, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import os
  2. import string
  3. import random
  4. import time
  5. import subprocess
  6.  
  7. # Generate a random password
  8. def randomPassword(stringLength=6):
  9. lettersAndDigits = string.ascii_letters + string.digits # Store digits and letters in ASCII
  10. return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))
  11.  
  12. user_amount = int(input('How many users? '))
  13.  
  14. # Create file to store user information
  15. users = open('users.txt', 'x')
  16. usernames = []
  17.  
  18.  
  19. # Create as many users as specified in user_amount
  20. for i in range (1, user_amount+1):
  21. # User information
  22. # users.write(f'user{i}:{randomPassword(8)}:{1000+user_amount}:{1000+user_amount}::/home/user{i}:/bin/bash\n')
  23. users.write('user{}:{}:{}:{}::/home/user{}:/bin/bash\n'.format(i, randomPassword(8), 1000+user_amount, 1000+user_amount, i))
  24. user = 'user{}'.format(i)
  25. usernames.append(user)
  26. # print(f'user{i}:{randomPassword(8)}:{100+user_amount}:{100+user_amount}::/home/user{i}:/bin/bash')
  27.  
  28. print('Changing permission for users.txt')
  29. time.sleep(1)
  30.  
  31. # Change permission of file so that only root can execute it
  32. os.system('sudo chmod 0600 users.txt')
  33.  
  34. # make_users = open('make_users.py', 'x')
  35.  
  36. # Create file that creates users
  37. # make_users.write('#!/usr/bin/env python3\n\n\
  38. # import os\n\n\
  39. # os.system(\'sudo newusers users.txt\')')
  40.  
  41. print('Creating all the users')
  42. time.sleep(2)
  43. # Run the file using newusers to create the users
  44. # os.system('python3 make_users.py')
  45.  
  46. # create_users = subprocess.call(['sudo', 'newusers', 'users.txt'])
  47.  
  48. create_users = subprocess.Popen(['sudo', 'newusers', 'users.txt'],
  49. shell=True,
  50. stdout=subprocess.PIPE,
  51. stderr=subprocess.STDOUT)
  52.  
  53. stdout,stderr = create_users.communicate()
  54. print(stdout)
  55. print(stderr)
  56.  
  57. print('All users created succesfully\n')
  58. print('Changing permissions of users')
  59.  
  60. # Change permissions of all users so they are not able to access each other
  61. for i in usernames:
  62. os.system('sudo chmod 0750 /home/{}'.format(i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement