473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Proper way to query user and group database on a Unix host?

Hi folks,

What's the proper way to query the passwd and group database on a Unix
host?

I'd like to fetch the users in a group (obviously from name services),
but my many varied searches can't find any reference of someone ever
looking up users on a Unix system, just NT. Weird, I know.

Currently I'm calling the getent command, which works well enough, but
surely there's a more Pythonic method of looking up OS user and group
data ...

## Get the full group database entry, leave just the user list, and split the list on comma
groupname=users
groupsusers = commands.getoutput('getent group '+groupname).split(':',-1)[3].split(',')
Cheers,

Mike

________________________________________________
Mike MacCana
Technical Specialist
Australia Linux and Virtualisation Services

IBM Global Services
Level 14, 60 City Rd
Southgate Vic 3000

Phone: +61-3-8656-2138
Fax: +61-3-8656-2423
Email: mm******@au1.ibm.com

Jul 23 '08 #1
4 2354
Mike MacCana <mm******@au1.ibm.comwrites:
Hi folks,

What's the proper way to query the passwd and group database on a Unix
host?
Use the pwd and grp modules, respectively.
## Get the full group database entry, leave just the user list,
## and split the list on comma
groupname=users
groupsusers = commands.getoutput('getent group '+groupname).split(':',-1)[3].split(',')
Instead, do this:

import grp
groupname = 'users'
groupusers = grp.getgrnam(groupname)[3]
print 'The group named "users" contains:'
for username in groupusers:
print username

The functions from the grp and pwd modules return tuples. The docs describe
their formats.

Hope this helps,
-- Chris
Jul 23 '08 #2
Chris Brannon <cm*******@cox.net>:

Iirc since Python 2.5 these tuples are named ...
Instead, do this:

import grp
groupname = 'users'
groupusers = grp.getgrnam(groupname)[3]
.... thus this line could be written as:

groupusers = grp.getgrnam(groupname).gr_mem

Slightly more readable, imho

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jul 23 '08 #3
On Wed, Jul 23, 2008 at 9:16 AM, Sebastian lunar Wiesner
<ba***********@gmx.netwrote:
Chris Brannon <cm*******@cox.net>:

Iirc since Python 2.5 these tuples are named ...
>Instead, do this:

import grp
groupname = 'users'
groupusers = grp.getgrnam(groupname)[3]
... thus this line could be written as:

groupusers = grp.getgrnam(groupname).gr_mem
That is valid since Python 2.3 actually
Slightly more readable, imho

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
--
http://mail.python.org/mailman/listinfo/python-list


--
-- Guilherme H. Polo Goncalves
Jul 23 '08 #4
Guilherme Polo <gg****@gmail.com>:
On Wed, Jul 23, 2008 at 9:16 AM, Sebastian lunar Wiesner
<ba***********@gmx.netwrote:
>Chris Brannon <cm*******@cox.net>:

Iirc since Python 2.5 these tuples are named ...
>>Instead, do this:

import grp
groupname = 'users'
groupusers = grp.getgrnam(groupname)[3]
... thus this line could be written as:

groupusers = grp.getgrnam(groupname).gr_mem

That is valid since Python 2.3 actually
Thanks for clarification
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jul 23 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: peterlin | last post by:
hi, How can I query the $ORACLE_HOME in sqlplus ?
3
by: Nick Truscott | last post by:
<? // scoreinput.php - input a match score when match selected from list ?> <html> <head> <basefont face="Verdana"> </head> <body>
0
by: phlype.johnson | last post by:
I'm struggling to find the best query from performance point of view and readability for a normalized DB design. To illustrate better my question on whether normalized designs lead to more complex...
0
by: Lester Knutsen | last post by:
A two-day IBM Informix and DB2 User Group Technical Conference - Friday and Saturday, December 8-9, 2006 We are very please to announce our Keynote Speakers...
0
by: Lester Knutsen | last post by:
A two-day IBM Informix and DB2 User Group Technical Conference - Friday and Saturday, December 8-9, 2006 Location - Fairview Park Marriott, Falls Church, VA (near Washington D.C.)...
2
by: jmarr02s | last post by:
I am creating a Pass Through Query. Here is my code: SELECT MDSDBA_CINTAKE.RECVDATE, MDSDBA_CINTAKE.CMPSRC, Count(MDSDBA_CINTAKE.CMPSRC) AS CountOfCMPSRC FROM MDSDBA_CINTAKE WHERE...
1
by: rajivblr | last post by:
I have my server on AS400. I connect to it using DB2Connect. In the AS400 server I have three kinds of tables one for English, one for Chinese and one for Thai. I need to use the...
0
by: Fredrik Lundh | last post by:
Mike MacCana wrote: the relevant modules are "pwd", "spwd" and "grp"; see: http://docs.python.org/lib/unix.html </F>
9
by: Sam Lambson | last post by:
Hi, This is similar to another post, because I still haven't gotten to the bottom of it. I have a back end database on a shared drive. Each (of 4) user has a front end file on their own C:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.