Guest User

Untitled

a guest
Mar 7th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. 1. create new Linux user with root privileges
  4. 2. remove a Linux user
  5. """
  6. import crypt
  7. import subprocess
  8. import getpass
  9.  
  10.  
  11. def useradd(username, user_password):
  12. """
  13. create user
  14. -m create the user's home directory
  15. """
  16. encPass = crypt.crypt(user_password, "22")
  17.  
  18. sudo_password = getpass.getpass("Enter your password: ")
  19. command = ("useradd -m -p " + encPass + " " + username + " -G sudo").split()
  20.  
  21. p = subprocess.Popen(['sudo', '-S'] + command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  22. sudo_prompt = p.communicate(sudo_password + '\n')[1]
  23. print(sudo_prompt)
  24. print('user created')
  25.  
  26.  
  27. def userdel(username):
  28. """
  29. remove user
  30. -r option to remove the users's home directory and mail spool
  31. """
  32. #sudo_password = input("Please enter your Password: ")
  33. sudo_password = getpass.getpass()
  34. command = ("userdel -r " + username).split()
  35.  
  36. p = subprocess.Popen(['sudo', '-S'] + command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  37. sudo_prompt = p.communicate(sudo_password + '\n')[1]
  38. print(sudo_prompt)
  39. print('user removed')
  40.  
  41.  
  42. def main():
  43. print('Enter 1 to create new user.')
  44. print('Enter 2 to remove a user.')
  45. option = input('Option: ')
  46.  
  47. if option is "1":
  48. username = input('Enter a new username: ')
  49. user_password = getpass.getpass('Enter a password for the new user: ')
  50. useradd(username, user_password)
  51. elif option is "2":
  52. username = input('Enter a username: ')
  53. userdel(username)
  54. else: main()
  55.  
  56.  
  57. if __name__ == "__main__":
  58. main()
Advertisement
Add Comment
Please, Sign In to add comment