Add user to group

Hi
I am new to python and trying to understand a code that adds user to a group. The code was provided in sections and I am trying to understand if the sections of code are alternatives or it is something to replace or if it belongs to the block of code. I have separated the sections with *** on newline.

def add_user_to_group():
username = input(“Enter the name of the user that you wantto addto agroup: “)
output = subprocess.Popen(‘groups’, stdout=subprocess.PIPE).communicate()[0]
print(“Enter a list of groups to add the user to”)
print(“The list should be separatedby spaces, for example:\r\n group1 group2 group3”)
print(“The available groups are:\r\n ” + output)
chosenGroups = input(“Groups: “)


output = output.split(” “)
chosenGroups = chosenGroups.split(” “)
print(“Add To:”)
groupString = “”
for grp in chosenGroups:
for existingGrp in output:
if grp == existingGrp:
found = True
print(“-Existing Group : ” + grp)
groupString = groupString + grp + “,”
if found == False:
print(“-New Group : ” + grp)
groupString = groupString + grp + “,”
else:
found = False


groupString = groupString[:-1] + ” “
confirm = “”
while confirm != “Y” and confirm != “N” :
print(“Add user ‘” + username + “‘ to thesegroups? (Y/N)”)
confirm = input().upper()
if confirm == “N”:
print(“User ‘” + username + “‘ not added”)
elif confirm == “Y”:
os.system(“sudo usermod -aG ” + groupString + username)
print(“User ‘” + username + ‘” added)


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
These qualities make cloudy a great pick for anyone needing an affordable, user friendly, and secure cloud storage solution. Install docker compose ubuntu. Privacy, cookie & data policy.