Python uses PWD and GRP to manipulate UNIX users and user groups

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.