Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Python script to create and delete sudo user  (Read 4390 times)

0 Members and 1 Guest are viewing this topic.

vichithrakumart

  • Guest
Python script to create and delete sudo user
« on: March 10, 2018, 11:36:37 am »
This script creates a user and adds them to the Wheel group

#!/usr/bin/env python
#sudo user creation
import sys
import subprocess
#import colored
while True:
        print "********Warning : Sudo users have special privileges. Be careful!!!!!!!********* "
        print "Sudo users list:"
        subprocess.call(["grep","wheel","/etc/group"])
        if len(sys.argv) > 1:
                name = sys.argv[1]
        else:
                name = raw_input('Enter User Name:')
        if name == 'root':
                print("Can't do anything with the 'root' user account......Breaking the program")
                sys.exit()
        opt = int(raw_input("Select option: 1. Create 2.Delete :"))
        if opt == 1:
                subprocess.call(["useradd", name])
                subprocess.call(["usermod", "-aG","wheel", name])
                subprocess.call(["passwd", name])
                print('\x1b[6;30;42m' + 'Sudo user creation is a Success!' + '\x1b[0m')
        elif opt == 2:
                subprocess.call(["userdel","-r", name])
                print('\x1b[6;30;42m' + 'Sudo user deletion is a Success!' + '\x1b[0m')
        else:
                print "Invalid option"
        op = raw_input("Do you want to continue (yes/no):")
        if op == 'no':
                sys.exit()