Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import string
- import random
- import time
- import subprocess
- # Generate a random password
- def randomPassword(stringLength=6):
- lettersAndDigits = string.ascii_letters + string.digits # Store digits and letters in ASCII
- return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))
- user_amount = int(input('How many users? '))
- # Create file to store user information
- users = open('users.txt', 'x')
- usernames = []
- # Create as many users as specified in user_amount
- for i in range (1, user_amount+1):
- # User information
- # users.write(f'user{i}:{randomPassword(8)}:{1000+user_amount}:{1000+user_amount}::/home/user{i}:/bin/bash\n')
- users.write('user{}:{}:{}:{}::/home/user{}:/bin/bash\n'.format(i, randomPassword(8), 1000+user_amount, 1000+user_amount, i))
- user = 'user{}'.format(i)
- usernames.append(user)
- # print(f'user{i}:{randomPassword(8)}:{100+user_amount}:{100+user_amount}::/home/user{i}:/bin/bash')
- print('Changing permission for users.txt')
- time.sleep(1)
- # Change permission of file so that only root can execute it
- os.system('sudo chmod 0600 users.txt')
- # make_users = open('make_users.py', 'x')
- # Create file that creates users
- # make_users.write('#!/usr/bin/env python3\n\n\
- # import os\n\n\
- # os.system(\'sudo newusers users.txt\')')
- print('Creating all the users')
- time.sleep(2)
- # Run the file using newusers to create the users
- # os.system('python3 make_users.py')
- # create_users = subprocess.call(['sudo', 'newusers', 'users.txt'])
- create_users = subprocess.Popen(['sudo', 'newusers', 'users.txt'],
- shell=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- stdout,stderr = create_users.communicate()
- print(stdout)
- print(stderr)
- print('All users created succesfully\n')
- print('Changing permissions of users')
- # Change permissions of all users so they are not able to access each other
- for i in usernames:
- os.system('sudo chmod 0750 /home/{}'.format(i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement