I've searched everywhere but haven't been able to find anything to do this in perl.
Thanks
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
Eric Schwartz wrote:
> Brandon Hoppe <bho...@ti.com> writes:
>
>>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`;
Shoot...didn't even think of that.
>
> ?
>
>
>>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
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.
> 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;
}
}
--
Aaron -- aaron_...@yahoo.com
http://360.yahoo.com/aaron_baugher
<snip>
> 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 ' ', $);
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
my @groups = split ' ', $(;
Or am I missing something?
HTH, Lukas
Yes, the rest of this thread.
http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/b9b4eeb7aabaf998
And the fact that $( consists of group IDs as opposed to group names.
my @groups = map scalar getgrgid $_, split ' ', $('
John
--
use Perl;
program
fulfillment
> if ( $users =~ /\b$user\b/ ) {
> push @groups, $group;
> }
Those \b can bite you with usernames that are beyond /\w+/.
--
Affijn, Ruud
"Gewoon is een tijger."
my @groups = `groups`;
or, my preference:
my @groups = qx/groups/;
--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas