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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike MacCana

    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.getout put('getent group '+groupname).sp lit(':',-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: mmaccana@au1.ib m.com

  • Chris Brannon

    #2
    Re: Proper way to query user and group database on a Unix host?

    Mike MacCana <mmaccana@au1.i bm.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.getout put('getent group '+groupname).sp lit(':',-1)[3].split(',')
    Instead, do this:

    import grp
    groupname = 'users'
    groupusers = grp.getgrnam(gr oupname)[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

    Comment

    • Sebastian \lunar\ Wiesner

      #3
      Re: Proper way to query user and group database on a Unix host?

      Chris Brannon <cmbrannon@cox. net>:

      Iirc since Python 2.5 these tuples are named ...
      Instead, do this:
      >
      import grp
      groupname = 'users'
      groupusers = grp.getgrnam(gr oupname)[3]
      .... thus this line could be written as:

      groupusers = grp.getgrnam(gr oupname).gr_mem

      Slightly more readable, imho



      --
      Freedom is always the freedom of dissenters.
      (Rosa Luxemburg)

      Comment

      • Guilherme Polo

        #4
        Re: Proper way to query user and group database on a Unix host?

        On Wed, Jul 23, 2008 at 9:16 AM, Sebastian lunar Wiesner
        <basti.wiesner@ gmx.netwrote:
        Chris Brannon <cmbrannon@cox. net>:
        >
        Iirc since Python 2.5 these tuples are named ...
        >
        >Instead, do this:
        >>
        >import grp
        >groupname = 'users'
        >groupusers = grp.getgrnam(gr oupname)[3]
        ... thus this line could be written as:
        >
        groupusers = grp.getgrnam(gr oupname).gr_mem
        >
        That is valid since Python 2.3 actually
        Slightly more readable, imho
        >
        >
        >
        --
        Freedom is always the freedom of dissenters.
        (Rosa Luxemburg)
        --

        >


        --
        -- Guilherme H. Polo Goncalves

        Comment

        • Sebastian \lunar\ Wiesner

          #5
          Re: Proper way to query user and group database on a Unix host?

          Guilherme Polo <ggpolo@gmail.c om>:
          On Wed, Jul 23, 2008 at 9:16 AM, Sebastian lunar Wiesner
          <basti.wiesner@ gmx.netwrote:
          >Chris Brannon <cmbrannon@cox. net>:
          >>
          >Iirc since Python 2.5 these tuples are named ...
          >>
          >>Instead, do this:
          >>>
          >>import grp
          >>groupname = 'users'
          >>groupusers = grp.getgrnam(gr oupname)[3]
          >... thus this line could be written as:
          >>
          >groupusers = grp.getgrnam(gr oupname).gr_mem
          >>
          >
          That is valid since Python 2.3 actually
          Thanks for clarification


          --
          Freedom is always the freedom of dissenters.
          (Rosa Luxemburg)

          Comment

          Working...