- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Access to the Group Database in Python
To access the UNIX group database, we should use the grp module. The shadow password database entries are like tuple like object.
To use the grp module, we should import it using −
import grp
The attributes of the grp database are −
Index | Attribute & Description |
---|---|
0 | gr_name The Name of the groups |
1 | gr_passwd The Encrypted password for the group. (Generally empty) |
2 | gr_gid The group id (Numeric) |
3 | gr_mem A list of group users |
In the group object, the gid is an integer. The group name and the password are strings. The Member list is a list of strings.
Some methods of this module are −
Method grp.getgrgid(gid)
This method will return group database entry from the given group id. When there is no group corresponds to gid, it will raise KeyError.
Method grp.getgrnam(name)
This method will return group database entry from the given group name. When there is no group corresponds to gid, it will raise KeyError.
Method grp.getgrall()
This method will return all group database entry.
Example Code
import grp print("ID: 4: " + str(grp.getgrgid(4)) + '\n') #Password detail using Group ID print("cdrom group: " + str(grp.getgrnam('cdrom')) + '\n') #Password detail using Group name for entry in grp.getgrall(): print("Group Name: " + entry[0] + "\t\tMembers: " + str(entry.gr_mem))
Output
$ sudo python3 example.py ID: 4: grp.struct_group(gr_name='adm', gr_passwd='x', gr_gid=4, gr_mem=['syslog', 'unix_user']) cdrom group: grp.struct_group(gr_name='cdrom', gr_passwd='x', gr_gid=24, gr_mem=['unix_user']) Group Name: root Members: [] Group Name: daemon Members: [] Group Name: bin Members: [] Group Name: sys Members: [] Group Name: adm Members: ['syslog', 'unix_user'] Group Name: tty Members: [] Group Name: disk Members: [] Group Name: lp Members: [] Group Name: mail Members: [] Group Name: news Members: [] Group Name: uucp Members: [] Group Name: man Members: [] Group Name: proxy Members: [] Group Name: kmem Members: [] Group Name: dialout Members: [] Group Name: fax Members: [] Group Name: voice Members: [] Group Name: cdrom Members: ['unix_user'] Group Name: floppy Members: [] Group Name: tape Members: [] Group Name: sudo Members: ['unix_user'] Group Name: audio Members: ['pulse'] Group Name: dip Members: ['unix_user'] Group Name: www-data Members: [] Group Name: backup Members: [] ……….. ……….. ………..
Advertisements