[code.view]

[top] / python / PyMOTW / docs / grp / index.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>grp – Unix Group Database &mdash; Python Module of the Week</title>
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.132',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="top" title="Python Module of the Week" href="../index.html" />
    <link rel="up" title="Unix-specific Services" href="../unix.html" />
    <link rel="next" title="pipes – Unix shell command pipeline templates" href="../pipes/index.html" />
    <link rel="prev" title="commands – Run external shell commands" href="../commands/index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../pipes/index.html" title="pipes – Unix shell command pipeline templates"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../commands/index.html" title="commands – Run external shell commands"
             accesskey="P">previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="../unix.html" accesskey="U">Unix-specific Services</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">grp &#8211; Unix Group Database</a><ul>
<li><a class="reference internal" href="#querying-all-groups">Querying All Groups</a></li>
<li><a class="reference internal" href="#group-memberships-for-a-user">Group Memberships for a User</a></li>
<li><a class="reference internal" href="#finding-a-group-by-name">Finding a Group By Name</a></li>
<li><a class="reference internal" href="#finding-a-group-by-id">Finding a Group by ID</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../commands/index.html"
                        title="previous chapter">commands &#8211; Run external shell commands</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../pipes/index.html"
                        title="next chapter">pipes &#8211; Unix shell command pipeline templates</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/grp/index.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-grp">
<span id="grp-unix-group-database"></span><h1>grp &#8211; Unix Group Database<a class="headerlink" href="#module-grp" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Purpose:</th><td class="field-body">Read group data from Unix group database.</td>
</tr>
<tr class="field"><th class="field-name">Python Version:</th><td class="field-body">1.4 and later</td>
</tr>
</tbody>
</table>
<p>The grp module can be used to read information about Unix groups from
the group database (usually <tt class="docutils literal"><span class="pre">/etc/group</span></tt>).  The read-only interface
returns tuple-like objects with named attributes for the standard
fields of a group record.</p>
<table border="1" class="docutils">
<colgroup>
<col width="12%" />
<col width="21%" />
<col width="67%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Index</th>
<th class="head">Attribute</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>0</td>
<td>gr_name</td>
<td>Name</td>
</tr>
<tr><td>1</td>
<td>gr_passwd</td>
<td>Password, if any (encrypted)</td>
</tr>
<tr><td>2</td>
<td>gr_gid</td>
<td>Numerical id (integer)</td>
</tr>
<tr><td>3</td>
<td>gr_mem</td>
<td>Names of group members</td>
</tr>
</tbody>
</table>
<p>The name and password values are both strings, the GID is an integer,
and the members are reported as a list of strings.</p>
<div class="section" id="querying-all-groups">
<h2>Querying All Groups<a class="headerlink" href="#querying-all-groups" title="Permalink to this headline">¶</a></h2>
<p>Suppose you need to print a report of all of the &#8220;real&#8221; groups on a
system, including their members (for our purposes, &#8220;real&#8221; is defined
as having a name not starting with &#8220;<tt class="docutils literal"><span class="pre">_</span></tt>&#8221;).  To load the entire
password database, you would use <tt class="docutils literal"><span class="pre">getgrall()</span></tt>.  The return value is
a list with an undefined order, so you probably want to sort it before
printing the report.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">grp</span>
<span class="kn">import</span> <span class="nn">operator</span>

<span class="c"># Load all of the user data, sorted by username</span>
<span class="n">all_groups</span> <span class="o">=</span> <span class="n">grp</span><span class="o">.</span><span class="n">getgrall</span><span class="p">()</span>
<span class="n">interesting_groups</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">((</span><span class="n">g</span> 
                            <span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">all_groups</span> 
                            <span class="k">if</span> <span class="ow">not</span> <span class="n">g</span><span class="o">.</span><span class="n">gr_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;_&#39;</span><span class="p">)),</span>
                            <span class="n">key</span><span class="o">=</span><span class="n">operator</span><span class="o">.</span><span class="n">attrgetter</span><span class="p">(</span><span class="s">&#39;gr_name&#39;</span><span class="p">))</span>

<span class="c"># Find the longest length for the name</span>
<span class="n">name_length</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">g</span><span class="o">.</span><span class="n">gr_name</span><span class="p">)</span> <span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">interesting_groups</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>

<span class="c"># Print report headers</span>
<span class="n">fmt</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%-*s</span><span class="s"> </span><span class="si">%4s</span><span class="s"> </span><span class="si">%10s</span><span class="s"> </span><span class="si">%s</span><span class="s">&#39;</span>
<span class="k">print</span> <span class="n">fmt</span> <span class="o">%</span> <span class="p">(</span><span class="n">name_length</span><span class="p">,</span> <span class="s">&#39;Name&#39;</span><span class="p">,</span> 
             <span class="s">&#39;GID&#39;</span><span class="p">,</span> 
             <span class="s">&#39;Password&#39;</span><span class="p">,</span>
             <span class="s">&#39;Members&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;-&#39;</span> <span class="o">*</span> <span class="n">name_length</span><span class="p">,</span> <span class="s">&#39;----&#39;</span><span class="p">,</span> <span class="s">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">10</span><span class="p">,</span> <span class="s">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">30</span>

<span class="c"># Print the data</span>
<span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">interesting_groups</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">fmt</span> <span class="o">%</span> <span class="p">(</span><span class="n">name_length</span><span class="p">,</span> <span class="n">g</span><span class="o">.</span><span class="n">gr_name</span><span class="p">,</span> 
                 <span class="n">g</span><span class="o">.</span><span class="n">gr_gid</span><span class="p">,</span> 
                 <span class="n">g</span><span class="o">.</span><span class="n">gr_passwd</span><span class="p">,</span>
                 <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">g</span><span class="o">.</span><span class="n">gr_mem</span><span class="p">))</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python grp_getgrall.py

Name                                      GID   Password Members
---------------------------------------- ---- ---------- ------------------------------
accessibility                              90          *
admin                                      80          * root, dhellmann
authedusers                                50          *
bin                                         7          *
certusers                                  29          * root, _jabber, _postfix, _cyrus, _calendar, _dovecot
com.apple.access_screensharing-disabled   101            dhellmann
com.apple.access_ssh                      102            dhellmann
consoleusers                               53          *
daemon                                      1          * root
dhellmann                                 501
dialer                                     68          *
everyone                                   12          *
group                                      16          *
interactusers                              51          *
kmem                                        2          * root
localaccounts                              61          *
mail                                        6          * _teamsserver
netaccounts                                62          *
netusers                                   52          *
network                                    69          *
nobody                                   4294967294          *
nogroup                                  4294967295          *
operator                                    5          * root
owner                                      10          *
postgres                                  401
procmod                                     9          * root
procview                                    8          * root
racemi                                    500            dhellmann
smmsp                                      25          *
staff                                      20          * root
sys                                         3          * root
tty                                         4          * root
utmp                                       45          *
wheel                                       0          * root</pre>
</div>
</div>
<div class="section" id="group-memberships-for-a-user">
<h2>Group Memberships for a User<a class="headerlink" href="#group-memberships-for-a-user" title="Permalink to this headline">¶</a></h2>
<p>Another common task might be to print a list of all the groups for a
given user:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">grp</span>

<span class="n">username</span> <span class="o">=</span> <span class="s">&#39;dhellmann&#39;</span>
<span class="n">groups</span> <span class="o">=</span> <span class="p">[</span><span class="n">g</span><span class="o">.</span><span class="n">gr_name</span> <span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">grp</span><span class="o">.</span><span class="n">getgrall</span><span class="p">()</span> <span class="k">if</span> <span class="n">username</span> <span class="ow">in</span> <span class="n">g</span><span class="o">.</span><span class="n">gr_mem</span><span class="p">]</span>
<span class="k">print</span> <span class="n">username</span><span class="p">,</span> <span class="s">&#39;belongs to:&#39;</span><span class="p">,</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">groups</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python grp_groups_for_user.py

dhellmann belongs to: _lpadmin, admin, com.apple.access_screensharing-disabled, com.apple.access_ssh, racemi</pre>
</div>
</div>
<div class="section" id="finding-a-group-by-name">
<h2>Finding a Group By Name<a class="headerlink" href="#finding-a-group-by-name" title="Permalink to this headline">¶</a></h2>
<p>As with <a class="reference internal" href="../pwd/index.html#module-pwd" title="pwd: Unix Password Database"><tt class="xref py py-mod docutils literal"><span class="pre">pwd</span></tt></a>, it is also possible to query for information about
a specific group, either by name or numeric id.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">grp</span>

<span class="n">name</span> <span class="o">=</span> <span class="s">&#39;admin&#39;</span>
<span class="n">info</span> <span class="o">=</span> <span class="n">grp</span><span class="o">.</span><span class="n">getgrnam</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;Name    :&#39;</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">gr_name</span>
<span class="k">print</span> <span class="s">&#39;GID     :&#39;</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">gr_gid</span>
<span class="k">print</span> <span class="s">&#39;Password:&#39;</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">gr_passwd</span>
<span class="k">print</span> <span class="s">&#39;Members :&#39;</span><span class="p">,</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">gr_mem</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python grp_getgrnam.py

Name    : admin
GID     : 80
Password: *
Members : root, dhellmann</pre>
</div>
</div>
<div class="section" id="finding-a-group-by-id">
<h2>Finding a Group by ID<a class="headerlink" href="#finding-a-group-by-id" title="Permalink to this headline">¶</a></h2>
<p>To identify the group running the current process, combine
<tt class="docutils literal"><span class="pre">getgrgid()</span></tt> with <tt class="docutils literal"><span class="pre">os.getgid()</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">grp</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">gid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getgid</span><span class="p">()</span>
<span class="n">group_info</span> <span class="o">=</span> <span class="n">grp</span><span class="o">.</span><span class="n">getgrgid</span><span class="p">(</span><span class="n">gid</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;Currently running with GID=</span><span class="si">%s</span><span class="s"> name=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">group_info</span><span class="o">.</span><span class="n">gr_name</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python grp_getgrgid_process.py

Currently running with GID=501 name=dhellmann</pre>
</div>
<p>And to get the group name based on the permissions on a file, look up
the group returned by <tt class="docutils literal"><span class="pre">os.stat()</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">grp</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="n">filename</span> <span class="o">=</span> <span class="s">&#39;grp_getgrgid_fileowner.py&#39;</span>
<span class="n">stat_info</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="n">owner</span> <span class="o">=</span> <span class="n">grp</span><span class="o">.</span><span class="n">getgrgid</span><span class="p">(</span><span class="n">stat_info</span><span class="o">.</span><span class="n">st_gid</span><span class="p">)</span><span class="o">.</span><span class="n">gr_name</span>

<span class="k">print</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> is owned by </span><span class="si">%s</span><span class="s"> (</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">owner</span><span class="p">,</span> <span class="n">stat_info</span><span class="o">.</span><span class="n">st_gid</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python grp_getgrgid_fileowner.py

grp_getgrgid_fileowner.py is owned by dhellmann (501)</pre>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://docs.python.org/library/grp.html">grp</a></dt>
<dd>The standard library documentation for this module.</dd>
<dt><a class="reference internal" href="../pwd/index.html#module-pwd" title="pwd: Unix Password Database"><tt class="xref py py-mod docutils literal"><span class="pre">pwd</span></tt></a></dt>
<dd>Read user data from the password database.</dd>
<dt><tt class="xref py py-mod docutils literal"><span class="pre">spwd</span></tt></dt>
<dd>Read user data from the shadow password database.</dd>
</dl>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../pipes/index.html" title="pipes – Unix shell command pipeline templates"
             >next</a> |</li>
        <li class="right" >
          <a href="../commands/index.html" title="commands – Run external shell commands"
             >previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="../unix.html" >Unix-specific Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright Doug Hellmann.
      Last updated on Oct 24, 2010.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.

    <br/><a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/88x31.png"/></a>
    
    </div>
  </body>
</html>

[top] / python / PyMOTW / docs / grp / index.html

contact | logmethods.com