Python script to specify users and groups

I am trying to encode a script that displays each user and their group on their own line as follows:

user1 group1 user2 group1 user3 group2 ... user10 group6 

etc..

I am writing a script in python for this, but wondered how SO can do this.

ps Take the punch in any language, but I would prefer python.

EDIT: I am working on Linux. Ubuntu 8.10 or CentOS =)

+20
python linux scripting sysadmin user
Jan 07 '09 at 19:05
source share
6 answers

For * nix, you have pwd and grp . You will follow through pwd.getpwall() to get all users. You are viewing your group names with grp.getgrgid(gid) .

 import pwd, grp for p in pwd.getpwall(): print p[0], grp.getgrgid(p[3])[0] 
+22
Jan 07 '09 at 19:14
source share

grp module is your friend. See grp.getgrall() for a list of all groups and their members.

EDIT example:

 import grp groups = grp.getgrall() for group in groups: for user in group[3]: print user, group[0] 
+14
Jan 07 '09 at 19:12
source share

w / bash:

 getent passwd | cut -f1 -d: | while read name; do echo -n "$name " ; groups $name ; done 
+5
Jan 07 '09 at 19:19
source share

The python call grp.getgrall() shows only local groups, unlike the call to the getgrouplist c function, which restarts all users, for example, also users in sssd, which is supported by ldap, but the listing is disabled. (as in FreeIPA). After finding the easiest way to get all the groups the users belong to in python, the best way I've found is to call the getgrouplist c function:

 #!/usr/bin/python import grp, pwd, os from ctypes import * from ctypes.util import find_library libc = cdll.LoadLibrary(find_library('libc')) getgrouplist = libc.getgrouplist # 50 groups should be enought? ngroups = 50 getgrouplist.argtypes = [c_char_p, c_uint, POINTER(c_uint * ngroups), POINTER(c_int)] getgrouplist.restype = c_int32 grouplist = (c_uint * ngroups)() ngrouplist = c_int(ngroups) user = pwd.getpwuid(2540485) ct = getgrouplist(user.pw_name, user.pw_gid, byref(grouplist), byref(ngrouplist)) # if 50 groups was not enough this will be -1, try again # luckily the last call put the correct number of groups in ngrouplist if ct < 0: getgrouplist.argtypes = [c_char_p, c_uint, POINTER(c_uint *int(ngrouplist.value)), POINTER(c_int)] grouplist = (c_uint * int(ngrouplist.value))() ct = getgrouplist(user.pw_name, user.pw_gid, byref(grouplist), byref(ngrouplist)) for i in xrange(0, ct): gid = grouplist[i] print grp.getgrgid(gid).gr_name 

To get a list of all users for whom this function was run in a similar way, you would need to find out what call c was made by getent passwd and call it in python.

+2
Apr 23 '18 at 10:39
source share

I believe this code meets your needs simply by using the basic functions of the Python interpreter, without the need for additional modules:

A simple function that can work with the structure of any of these files (/ etc / passwd and / etc / group).

Here is the code:

 #!/usr/bin/python data = [] def iterator(f): for line in f.readlines(): data.append(line.split(":")[0]) data.sort() for item in data: print("- " + item) with open("/etc/group","r") as f: print("\n* GROUPS *") iterator(f) print() with open("/etc/passwd","r") as f: print("\n* USERS *") iterator(f) 
0
Apr 08 '16 at 20:29
source share

Fed for os.popen or something else ....

"groups of people"

GROUPS (1) Teams user groups (1)

NAME groups - print groups the user is in

SYNTAX of groups [OPTION] ... [USERNAME] ...

DESCRIPTION Select a group membership for each USERNAME user or, if USERNAME is not specified, for the current process (which may be different if the group has changed).

....

-2
Nov 02 '15 at 22:43
source share



All Articles