21.2. pwd — unix 密码数据库 | unix 专属服务 |《python 3 标准库实例教程》| python 技术论坛-380玩彩网官网入口

未匹配的标注

目的:从 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

参见如下

  • spwd -- 安全密码数据库访问,用于采用 shadow 文件保存密码的系统。
  •  -- the  读取 unix 用户组信息的模块。

本文章首发在 380玩彩网官网入口 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系380玩彩网官网入口。

原文地址:https://learnku.com/docs/pymotw/pwd-unix...

译文地址:https://learnku.com/docs/pymotw/pwd-unix...

上一篇 下一篇
讨论数量: 0



暂无话题~
网站地图