1. PWD Module
The PWD module provides an operating interface for the UNIX password database,/etc/passwd, which contains local machine user account information
Common operations are as follows:
Pwd.getpwuid (UID): Returns sample information for the corresponding UID
Example:
>>> pwd.getpwuid (0) pwd.struct_passwd (pw_name='root', pw_passwd ='x', Pw_uid=0, pw_gid=0, pw_gecos='root', pw_dir=' /root', pw_shell='/bin/bash')
Pwd.getpwnam (name): Returns user information corresponding to name
Example:
>>> Pwd.getpwnam ('root') pwd.struct_passwd (pw_name=' Root', pw_passwd='x', Pw_uid=0, pw_gid=0, pw_gecos='root ', pw_dir='/root', pw_shell='/bin/bash ')
Pwd.getpwall (): Returns all user information
Example:
Import pwd def get_user () = {} for in pwd.getpwall () = all_user[user[2]] = user return All_user def userinfo (UID): return get_user () [UID]
Execution Result:
Printuserinfo (0) pwd.struct_passwd (pw_name='Root', pw_passwd='x', Pw_uid=0, Pw_gid=0, pw_gecos='Root', pw_dir='/root', pw_shell='/bin/bash')PrintUserInfo'Root') pwd.struct_passwd (Pw_name='Root', pw_passwd='x', Pw_uid=0, Pw_gid=0, pw_gecos='Root', pw_dir='/root', pw_shell='/bin/bash')
2. GRP Module
The GRP module provides an interface for manipulating UNIX user groups, i.e., the/etc/group database.
Common operations are as follows:
Grp.getgrgid (GID): Returns the group information corresponding to the GID
Example:
Print grp.getgrgid (0) grp.struct_group (gr_name='root', gr_passwd='x ', Gr_gid=0, gr_mem=[])
Grp.getgrnam (name): Returns the group information corresponding to name
Example:
print Grp.getgrnam ('root') grp.struct_group (gr_name=' Root', gr_passwd='x', Gr_gid=0, gr_mem=[])
Grp.getgrall (): Returns all group information
Python uses PWD and GRP to manipulate UNIX users and user groups