I have thousands of /etc/group files in a directory and thousand of corresponding /etc/passwd files in another directory. Now, given a group I am trying to get all the primary and secondary members of that group. I have an example below:


Line from /etc/group:
samplegrp::12345:bobjones,davejones

Lines from /etc/passwd
davana:1111:12345:Dan Vana:/davana:/usr/bin/ksh
bobjones:2222:54321:Bob Jones:/bobjones:/usr/bin/ksh
davejones:3333:6789:Dave Jones:/davejones:/usr/bin/ksh

So, as you can see the GID for samplegrp is 12345. It has one primary member (davana), and two secondary members. Now, like I said, thousands of group files (for many different servers) and thousands of passwd files). So, samplegrp could exist on hundreds of servers. I want all the primary/secondary members for this group on all servers where it exists. I have included a quick sample script that shows how I am doing this, and just looking for some suggestions. Thank you guys!!

#!/usr/bin/perl -w use strict; system("clear"); print "Please enter the group name that you're querying: "; chomp (my $group_name=<STDIN>); my @servers=`grep "^$group_name:" group/* | cut -d/ -f2 | cut -d. -f1` +; foreach my $server (@servers) { chomp $server; my $group_line=`grep "^$group_name:" group/$server.grp`; my ($groupName,undef,$gid,$secondaryMembers)=split(/:/,$group_line +); my @primary_members=`egrep ".*:[^A-Za-z]+:$gid:.*" passwd/$server. +passwd`; if (@primary_members) { print "<@primary_members>\n"; } if (defined $secondaryMembers ) { my @users=split(/,/,$secondaryMembers); foreach my $user (@users) { chomp $user; my $userInfo=`grep "^$user:" passwd/$server.passwd`; if ($userInfo) { print "[$userInfo]\n"; } } } }
Just to let you know, I am not trying to query one at at time like the script above, but rather go through a file with 900 lines, each line has one group. Also, I could parse through each group file and its corresponding file, but that doesn't make a lot of sense if the group only exists on one server, right? And finally, just for clarification, below I have given you an idea of what the layout of the group/passwd directories looks like:

passwd directory:
server1.passwd
server2.passwd
server3.passwd

group directory:
server1.grp
server2.grp
server3.grp

So basically group samplegrp could exist only on server150 out of server4500 (that is the total amount of servers), therefore it does not make sense to parse every file.

In reply to Any way to do this without system calls? by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.