Help: How do I get the lists of groups a unix user is in?

B

Brandon Hoppe

I need the list of groups that a user is in, similar to calling "groups" at a command prompt.

I've searched everywhere but haven't been able to find anything to do this in perl.

Thanks
 
E

Eric Schwartz

Brandon Hoppe said:
I need the list of groups that a user is in, similar to calling "groups" at a command prompt.

What's wrong with

my @groups = split ' ', `groups`;

?
I've searched everywhere but haven't been able to find anything to do this in perl.

Have you tried anything? I don't mind tossing off a one-liner here or
there, but in general, people in this group resent being asked to do
someone else's work for them, but are more than happy to help correct
any code that you've tried, but failed, to write correctly.

-=Eric
 
B

Brandon Hoppe

Eric said:
What's wrong with

my @groups = split ' ', `groups`;

Shoot...didn't even think of that.
?




Have you tried anything? I don't mind tossing off a one-liner here or
there, but in general, people in this group resent being asked to do
someone else's work for them, but are more than happy to help correct
any code that you've tried, but failed, to write correctly.

-=Eric

Yeah, I've tried several combinations of the functions getpwuid(), getgrnam(), getgrgid()
etc. But using those I was only able to get the first group listed when you call groups. I
wasn't able to figure out what other group the user was associated with. For example:

my $group = getgrgid((getpwuid($<))[3]);

Only returned the 1st group name listed when you ran groups. Even if the user changed to a
group using 'newgrp' command, it wouldn't give the current group that the user was using.
 
A

Aaron Baugher

Brandon Hoppe said:
I need the list of groups that a user is in, similar to calling
"groups" at a command prompt.

The easy answer would be:

my @groups = split / /, `groups $username`;

If you want to do it all with perl functions, take a look at
getgrent(). It'll iterate through the group file, so you can do
something like this (untested):

my $user = 'foobar'; # username we want the groups for
my @groups; # list of groups to which $user belongs
while(my($group, $passwd, $gid, $users) = getgrent ){
if( $users =~ /\b$user\b/ ){
push @groups, $group;
}
}
 
G

Gunnar Hjalmarsson

Brandon said:
Shoot...didn't even think of that.

Yeah, I've tried several combinations of the functions getpwuid(),
getgrnam(), getgrgid() etc. But using those I was only able to get the
first group listed when you call groups. I wasn't able to figure out
what other group the user was associated with. For example:

my $group = getgrgid((getpwuid($<))[3]);

Only returned the 1st group name listed when you ran groups. Even if the
user changed to a group using 'newgrp' command, it wouldn't give the
current group that the user was using.

This comes close, without shelling out:

my @groups = map { (getgrgid $_)[0] } split ' ', $);
 
L

Lukas Mai

Brandon Hoppe said:
I need the list of groups that a user is in, similar to calling
"groups" at a command prompt.

I've searched everywhere but haven't been able to find anything to do
this in perl.

my @groups = split ' ', $(;

Or am I missing something?

HTH, Lukas
 
T

Tad McClellan

Brandon Hoppe said:
I need the list of groups that a user is in, similar to calling "groups" at a command prompt.

I've searched everywhere but haven't been able to find anything to do this in perl.


my @groups = `groups`;

or, my preference:

my @groups = qx/groups/;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top