Re: Displaying a user's group memberships
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Mon, 18 Dec 2006 17:26:53 -0800
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
.
- References:
- Displaying a user's group memberships
- From: Tom Smith
- Displaying a user's group memberships
- Prev by Date: Re: How do I tell perl -w: "I really want to use this var just once"
- Next by Date: Re: How do I tell perl -w: "I really want to use this var just once"
- Previous by thread: Re: Displaying a user's group memberships
- Next by thread: How do I tell perl -w: "I really want to use this var just once"
- Index(es):
Relevant Pages
|
|