用于列出用户和组的Python脚本

时间:2022-07-07 15:22:35

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.

我正在为python编写一个脚本但是想知道SO是如何做到这一点的。

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

附:用任何语言打击它,但我更喜欢python。

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

编辑:我正在研究Linux。 Ubuntu 8.10或CentOS =)

6 个解决方案

#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).

对于* nix,您有pwd和grp模块。您遍历pwd.getpwall()以获取所有用户。您使用grp.getgrgid(gid)查找其组名。

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

#2


the grp module is your friend. Look at grp.getgrall() to get a list of all groups and their members.

grp模块是你的朋友。查看grp.getgrall()以获取所有组及其成员的列表。

EDIT example:

import grp
groups = grp.getgrall()
for group in groups:
    for user in group[3]:
        print user, group[0]

#3


sh/bash:

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

#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:

对grp.getgrall()的python调用仅显示本地组,而不像调用getgrouplist c函数那样重新调用所有用户,例如: sdd中的用户也是由ldap支持但枚举已关闭的用户。 (就像在FreeIPA中一样)。在搜索了最简单的方法来获取用户所属的所有组后,我发现最好的方法是实际调用getgrouplist c函数:

#!/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 enought 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.

获取所有用户的列表以类似地运行此函数将需要找出getent passwd进行的c调用并在python中调用它。

#5


I believe that this code meets your need, just using the core functions of Python interpreter, without the need to make use of additional modules:

我相信这段代码只需要使用Python解释器的核心功能就可以满足您的需求,而无需使用其他模块:

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

一个简单的函数,能够处理这些文件中的任何一个(/ etc / passwd和/ 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);    

#6


Fed to os.popen or whatever....

加入os.popen或其他......

"man groups"

GROUPS(1) User Commands GROUPS(1)

组(1)用户命令组(1)

NAME groups - print the groups a user is in

名称组 - 打印用户所在的组

SYNOPSIS groups [OPTION]... [USERNAME]...

大纲组[选项] ... [用户名] ...

DESCRIPTION Print group memberships for each USERNAME or, if no USERNAME is specified, for the current process (which may differ if the groups database has changed).

描述为每个USERNAME打印组成员身份,如果没有指定USERNAME,则打印当前进程的组成员身份(如果组数据库已更改,则可能会有所不同)。

....

#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).

对于* nix,您有pwd和grp模块。您遍历pwd.getpwall()以获取所有用户。您使用grp.getgrgid(gid)查找其组名。

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

#2


the grp module is your friend. Look at grp.getgrall() to get a list of all groups and their members.

grp模块是你的朋友。查看grp.getgrall()以获取所有组及其成员的列表。

EDIT example:

import grp
groups = grp.getgrall()
for group in groups:
    for user in group[3]:
        print user, group[0]

#3


sh/bash:

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

#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:

对grp.getgrall()的python调用仅显示本地组,而不像调用getgrouplist c函数那样重新调用所有用户,例如: sdd中的用户也是由ldap支持但枚举已关闭的用户。 (就像在FreeIPA中一样)。在搜索了最简单的方法来获取用户所属的所有组后,我发现最好的方法是实际调用getgrouplist c函数:

#!/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 enought 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.

获取所有用户的列表以类似地运行此函数将需要找出getent passwd进行的c调用并在python中调用它。

#5


I believe that this code meets your need, just using the core functions of Python interpreter, without the need to make use of additional modules:

我相信这段代码只需要使用Python解释器的核心功能就可以满足您的需求,而无需使用其他模块:

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

一个简单的函数,能够处理这些文件中的任何一个(/ etc / passwd和/ 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);    

#6


Fed to os.popen or whatever....

加入os.popen或其他......

"man groups"

GROUPS(1) User Commands GROUPS(1)

组(1)用户命令组(1)

NAME groups - print the groups a user is in

名称组 - 打印用户所在的组

SYNOPSIS groups [OPTION]... [USERNAME]...

大纲组[选项] ... [用户名] ...

DESCRIPTION Print group memberships for each USERNAME or, if no USERNAME is specified, for the current process (which may differ if the groups database has changed).

描述为每个USERNAME打印组成员身份,如果没有指定USERNAME,则打印当前进程的组成员身份(如果组数据库已更改,则可能会有所不同)。

....