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

Discussion in 'Python' started by Mike MacCana, Jul 23, 2008.

  1. Mike MacCana

    Mike MacCana Guest

    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:
     
    Mike MacCana, Jul 23, 2008
    #1
    1. Advertising

  2. Mike MacCana <> writes:

    > 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
     
    Chris Brannon, Jul 23, 2008
    #2
    1. Advertising

  3. Chris Brannon <>:

    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)
     
    Sebastian \lunar\ Wiesner, Jul 23, 2008
    #3
  4. On Wed, Jul 23, 2008 at 9:16 AM, Sebastian lunar Wiesner
    <> wrote:
    > Chris Brannon <>:
    >
    > 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
     
    Guilherme Polo, Jul 23, 2008
    #4
  5. Guilherme Polo <>:

    > On Wed, Jul 23, 2008 at 9:16 AM, Sebastian lunar Wiesner
    > <> wrote:
    >> Chris Brannon <>:
    >>
    >> 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)
     
    Sebastian \lunar\ Wiesner, Jul 23, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Orpheus66
    Replies:
    0
    Views:
    4,442
    Orpheus66
    Jul 30, 2003
  2. unishippers.suckfeed.newshosting.com

    XSD - proper way to express group of elements...

    unishippers.suckfeed.newshosting.com, Nov 3, 2004, in forum: XML
    Replies:
    1
    Views:
    354
    Priscilla Walmsley
    Nov 4, 2004
  3. Benoit Lefebvre
    Replies:
    9
    Views:
    414
    Al Balmer
    Sep 7, 2007
  4. p.
    Replies:
    10
    Views:
    639
    Albert van der Horst
    Jan 28, 2008
  5. Jerry Qu
    Replies:
    1
    Views:
    494
    lee atkinson
    Feb 20, 2009
Loading...

Share This Page