Re: Displaying a user's group memberships



Tom Smith wrote:
I need to capture a user's group memberships for further processing in a
Perl script. The user's username is passed to the script via the command
line and captured with ARGV. From there, I want to determine the group
memberships (much like executing `groups` from the command line) and run
those through a loop for processing.

I seem to be having a problem locating a function that will do this.
I've looked at several and tried a couple, but either I'm doing
something wrong or I'm using the wrong functions. All of the ones I've
tried are part of getgr*.

I'm very new to Perl so maybe I'm just looking for the wrong terms or
something. I've googled and searched perldoc.perl.org.

Can anyone offer any suggestions or point me in the right direction?

Something like this should get you started:

my %user;

# get the group name from /etc/passwd
while ( my @pw = getpwent ) {
push @{ $user{ $pw[ 0 ] } }, scalar getgrgid $pw[ 3 ];
}

# get other group names from /etc/group
while ( my @gr = getgrent ) {
push @{ $user{ $_ } }, $gr[ 0 ] for split q/ /, $gr[ 3 ];
}

my $name = shift; # get the name from the command line

if ( exists $user{ $name } && @{ $user{ $name } } ) {
print "$name is a member of the groups: @{$user{$name}}.\n";
}




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.



Relevant Pages

  • RE: setting unix command through perl script
    ... I am automating one task in Perl in which this command is ... If I don't execute this command then entire task would fail. ... setting unix command through perl script ... To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx ...
    (perl.beginners)
  • Re: Problems running perl without .pl extention
    ... @ARGV provides only those arguments provided ... perl core and specific perl file. ... That will not run a perl script from a command line ...
    (comp.lang.perl.misc)
  • RE: setting unix command through perl script
    ... I am automating one task in Perl in which this command is ... If I don't execute this command then entire task would fail. ... setting unix command through perl script ...
    (perl.beginners)
  • Re: Displaying a users group memberships
    ... Perl script. ... The user's username is passed to the script via the command ... perldoc -f split ...
    (perl.beginners)
  • Re: Running DOS from MATLAB
    ... > This command runs a Perl script. ... > back to Matlab. ... still wait for the perl script to complete). ...
    (comp.soft-sys.matlab)