21.2. pwd — Unix 密码数据库
目的:从 Unix 密码数据库读取用户数据
pwd
模块能够用来从Unix密码数据库(通常是 /etc/passwd
)读取用户信息,以命名元组形式返回用户信息中的属性字段。
序号 | 属性 | 含义 |
---|---|---|
0 | pw_name |
用户登录名 |
1 | pw_passwd |
加密后的密码(可选,即可能是 * ) |
2 | pw_uid |
用户ID(整数) |
3 | pw_gid |
用户组ID(整数) |
4 | pw_gecos |
注释/用户全名 |
5 | pw_dir |
用户 Home 目录 |
6 | pw_shell |
用户登录后运行的程序,通常是命令解释器 |
查询所有用户
这个示例打印系统内所有 「真」 用户的信息,包括用户 Home 目录(「真」用户指用户名不是 _
开头的用户)。要加载整个密码数据库,使用 getpwall()
。返回值是未排序的列表,所以在打印之前要对结果进行排序。
pwd_getpwall.py
import pwd
import operator
# 加载所有用户数据,按用户名排序
all_user_data = pwd.getpwall()
interesting_users = sorted(
(u for u in all_user_data
if not u.pw_name.startswith('_')),
key=operator.attrgetter('pw_name')
)
# 查找某些字段的最大长度
username_length = max(len(u.pw_name)
for u in interesting_users) + 1
home_length = max(len(u.pw_dir)
for u in interesting_users) + 1
uid_length = max(len(str(u.pw_uid))
for u in interesting_users) + 1
# 打印表头
fmt = ' '.join(['{:<{username_length}}',
'{:>{uid_length}}',
'{:<{home_length}}',
'{}'])
print(fmt.format('User',
'UID',
'Home Dir',
'Description',
username_length=username_length,
uid_length=uid_length,
home_length=home_length))
print('-' * username_length,
'-' * uid_length,
'-' * home_length,
'-' * 20)
# 打印数据
for u in interesting_users:
print(fmt.format(u.pw_name,
u.pw_uid,
u.pw_dir,
u.pw_gecos,
username_length=username_length,
uid_length=uid_length,
home_length=home_length))
上述示例中绝大部分代码是为了处理打印格式。最后的 for
循环显示了怎么通过字段名称访问记录的属性。
$ python3 pwd_getpwall.py
User UID Home Dir Description
---------- ----------- ----------------- --------------------
Guest 201 /Users/Guest Guest User
daemon 1 /var/root System Services
daemon 1 /var/root System Services
dhellmann 501 /Users/dhellmann Doug Hellmann
nobody 4294967294 /var/empty Unprivileged User
nobody 4294967294 /var/empty Unprivileged User
root 0 /var/root System Administrator
root 0 /var/root System Administrator
根据用户名查找用户
获取一个用户的信息并不需要读取整个密码数据库,使用 getpwnam()
,可以根据用户名获取用户信息。
pwd_getpwnam.py
import pwd
import sys
username = sys.argv[1]
user_info = pwd.getpwnam(username)
print('Username:', user_info.pw_name)
print('Password:', user_info.pw_passwd)
print('Comment :', user_info.pw_gecos)
print('UID/GID :', user_info.pw_uid, '/', user_info.pw_gid)
print('Home :', user_info.pw_dir)
print('Shell :', user_info.pw_shell)
运行这个示例的系统上用户密码保存在单独的 shadow
文件中,所以 password
字段全部返回 *
。
$ python3 pwd_getpwnam.py dhellmann
Username: dhellmann
Password: ********
Comment : Doug Hellmann
UID/GID : 501 / 20
Home : /Users/dhellmann
Shell : /bin/bash
$ python3 pwd_getpwnam.py nobody
Username: nobody
Password: *
Comment : Unprivileged User
UID/GID : 4294967294 / 4294967294
Home : /var/empty
Shell : /usr/bin/false
根据用户ID查找用户
当然也可以根据用户 ID 查找用户信息。当根据文件所有者获取用户信息时会非常有用。
pwd_getpwuid_fileowner.py
import pwd
import os
filename = 'pwd_getpwuid_fileowner.py'
stat_info = os.stat(filename)
owner = pwd.getpwuid(stat_info.st_uid).pw_name
print('{} is owned by {} ({})'.format(
filename, owner, stat_info.st_uid))
$ python3 pwd_getpwuid_fileowner.py
pwd_getpwuid_fileowner.py is owned by dhellmann (501)
根据用户 ID 也可以获取运行当前进程的用户信息。
pwd_getpwuid_process.py
import pwd
import os
uid = os.getuid()
user_info = pwd.getpwuid(uid)
print('Currently running with UID={} username={}'.format(
uid, user_info.pw_name))
$ python3 pwd_getpwuid_process.py
Currently running with UID=501 username=dhellmann
参见如下
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。