按组名获取组 ID(Python、Unix)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1157864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get group id by group name (Python, Unix)
提问by SimonSalman
I want to use Python to get the group id to a corresponding group name. The routine must work for Unix-like OS (Linux and Mac OS X).
我想使用 Python 将组 id 获取到相应的组名。该例程必须适用于类 Unix 操作系统(Linux 和 Mac OS X)。
This is what I found so far
这是我目前发现的
>>> import grp
>>> for g in grp.getgrall():
... if g[0] == 'wurzel':
... print g[2]
回答by Martijn Pieters
If you read the grp module documentationyou'll see that grp.getgrnam(groupname) will return one entry from the group database, which is a tuple-like object. You can either access the information by index or by attribute:
如果您阅读grp 模块文档,您将看到 grp.getgrnam(groupname) 将从组数据库中返回一个条目,它是一个类似元组的对象。您可以通过索引或属性访问信息:
>>> import grp
>>> groupinfo = grp.getgrnam('root')
>>> print groupinfo[2]
0
>>> print groupinfo.gr_gid
0
Other entries are the name, the encrypted password (usually empty, if using a shadow file, it'll be a dummy value) and all group member names. This works fine on any Unix system, including my Mac OS X laptop:
其他条目是名称、加密密码(通常为空,如果使用影子文件,它将是一个虚拟值)和所有组成员名称。这适用于任何 Unix 系统,包括我的 Mac OS X 笔记本电脑:
>>> import grp
>>> admin = grp.getgrnam('admin')
>>> admin
('admin', '*', 80, ['root', 'admin', 'mj'])
>>> admin.gr_name
'admin'
>>> admin.gr_gid
80
>>> admin.gr_mem
['root', 'admin', 'mj']
The module also offers a method to get entries by gid, and as you discovered, a method to loop over all entries in the database:
该模块还提供了一种通过 gid 获取条目的方法,正如您发现的那样,该模块还提供了一种遍历数据库中所有条目的方法:
>>> grp.getgrgid(80)
('admin', '*', 80, ['root', 'admin', 'mj'])
>>> len(grp.getgrall())
73
Last but not least, python offers similar functionality to get information on the password and shadow files, in the pwdand spwdmodules, which have a similar API.
最后但同样重要的是,python 在pwd和spwd模块中提供了类似的功能来获取有关密码和影子文件的信息,它们具有类似的 API。
回答by gimel
See grp.getgrnam(name)
:
grp.getgrnam(name)
Return the group database entry for the given group name. KeyError is raised if the entry asked for cannot be found.
Group database entries are reported as a tuple-like object, whose attributes correspond to the members of the group structure:
grp.getgrnam(name)
返回给定组名的组数据库条目。如果找不到要求的条目,则会引发 KeyError。
组数据库条目报告为类似元组的对象,其属性对应于组结构的成员:
Index Attribute Meaning
0 gr_name the name of the group
1 gr_passwd the (encrypted) group password; often empty
2 gr_gid the numerical group ID
3 gr_mem all the group member's user names
The numerical group ID is at index 2, or 2nd from last, or the attribute gr_gid
.
数字组 ID 位于索引 2 处,或倒数第二个,或属性gr_gid
。
GID of root
is 0:
GIDroot
为 0:
>>> grp.getgrnam('root')
('root', 'x', 0, ['root'])
>>> grp.getgrnam('root')[-2]
0
>>> grp.getgrnam('root').gr_gid
0
>>>
回答by user11122920
Using perl:
使用 perl:
$grp_id = getgrnam($ARGV[0]);
print $grp_id."\n";