Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a user-group in linux using python

I want to create a user group using python on CentOS system. When I say 'using python' I mean I don't want to do something like os.system and give the unix command to create a new group. I would like to know if there is any python module that deals with this.

Searching on the net did not reveal much about what I want, except for python user groups.. so I had to ask this.

I learned about the grp module by searching here on SO, but couldn't find anything about creating a group.

EDIT: I dont know if I have to start a new question for this, but I would also like to know how to add (existing) users to the newly created group.

Any help appreciated. Thank you.

like image 929
sharat87 Avatar asked Oct 15 '09 05:10

sharat87


1 Answers

I don't know of a python module to do it, but the /etc/group and /etc/gshadow format is pretty standard, so if you wanted you could just open the files, parse their current contents and then add the new group if necessary.

Before you go doing this, consider:

  • What happens if you try to add a group that already exists on the system
  • What happens when multiple instances of your program try to add a group at the same time
  • What happens to your code when an incompatible change is made to the group format a couple releases down the line
  • NIS, LDAP, Kerberos, ...

If you're not willing to deal with these kinds of problems, just use the subprocess module and run groupadd. It will be way less likely to break your customers machines.

Another thing you could do that would be less fragile than writing your own would be to wrap the code in groupadd.c (in the shadow package) in Python and do it that way. I don't see this buying you much versus just exec'ing it, though, and it would add more complexity and fragility to your build.

like image 104
Jack Lloyd Avatar answered Sep 29 '22 09:09

Jack Lloyd