Python script to list users and groups

I'm attempting to code a script that outputs each user and their group on their own line like so:

user1 group1  
user2 group1  
user3 group2  
...  
user10 group6

etc.

I'm writing up a script in python for this but was wondering how SO might do this.

p.s. Take a whack at it in any language but I'd prefer python.

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


Solution 1:

For *nix, you have the pwd and grp modules. You iterate through pwd.getpwall() to get all users. You look up their group names with grp.getgrgid(gid).

import pwd, grp
for p in pwd.getpwall():
    print p[0], grp.getgrgid(p[3])[0]

Solution 2:

the grp module is your friend. Look at grp.getgrall() to get 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]

Solution 3:

sh/bash:

getent passwd | cut -f1 -d: | while read name; do echo -n "$name " ; groups $name ; done

Solution 4:

The python call to grp.getgrall() only shows the local groups, unlike the call to getgrouplist c function which retruns all users, e.g. also users in sssd that is backed by an ldap but has enumeration turned off. (like in FreeIPA). After searching for the easiest way to get all groups a users belongs to in python the best way I found was to actually 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

Getting a list of all users to run this function on similarly would require to figure out what c call is made by getent passwd and call that in python.

Solution 5:

a simple function which is capable to deal with the structure of any one of these files (/etc/passwd and /etc/group).

I believe that this code meets your needs, with Python built-in functions and no additional module:

#!/usr/bin/python


def read_and_parse(filename):
    """
        Reads and parses lines from /etc/passwd and /etc/group.

        Parameters

          filename : str
            Full path for filename.
    """
    data = []
    with open(filename, "r") as f:
        for line in f.readlines():
            data.append(line.split(":")[0])
        data.sort()
        for item in data:
            print("- " + item)


read_and_parse("/etc/group")
read_and_parse("/etc/passwd")