Getting the list of groups given a user id

A

arshad.tanveer

Hi,
I am working on a script that would run on Solaris and I need a Perl
script/function/module that would give me the list of groups that a
user belongs to- just like the Unix groups command. Could someone
please help?

Regards,
Tanveer
 
J

Josef Moellers

Hi,
I am working on a script that would run on Solaris and I need a Perl
script/function/module that would give me the list of groups that a
user belongs to- just like the Unix groups command. Could someone
please help?

Please have a look at the posting guidelines.
What have you tried so far and where did your code not meet your
expectations?
 
A

arshad.tanveer

Please have a look at the posting guidelines.
What have you tried so far and where did your code not meet your
expectations?

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett


I haven't tried anything because I don't even know where to start.
Googl'ing for answers, I came across functions like getgrnam but they
give a list of users in a group. I want it the other way round. Given
a user id, how to determine the groups?

Thanks,
Tanveer
 
J

Josef Moellers

I haven't tried anything because I don't even know where to start.
Googl'ing for answers, I came across functions like getgrnam but they
give a list of users in a group. I want it the other way round. Given
a user id, how to determine the groups?

You mean: you don't know anything about Perl? Or you don't know anything
about where Solaris keeps this information?

One of the major features of Perl are its text processing abilities.
Usually Un*x base systems keep the information about users and groups in
text files in /etc (I would be surprised if Solaris did otherwise).
So, adding 1(text processing features) and 1(information kept in text
files) together should get you started. If you have problems getting the
first line of Perl code into the machine or onto a sheet of paper, this
group is definitely the wrong place to start. Better get a decent book,
e.g. the camel book, and start reading.

You wrote "I'm working os a script ...", so you must at least have
something to start with.

Eeven if you post a rudimentary program that shows that you at least
tried to do some work yourself, you'll find legions of people willing to
help (well, maybe not legions).

This is not a group to ask for ready-to-use solutions.
 
M

Mumia W.

Hi,
I am working on a script that would run on Solaris and I need a Perl
script/function/module that would give me the list of groups that a
user belongs to- just like the Unix groups command. Could someone
please help?

Regards,
Tanveer

Read about the "getgrent" function:

Start->Run->"perldoc -f getgrent"
 
X

xhoster

Hi,
I am working on a script that would run on Solaris and I need a Perl
script/function/module that would give me the list of groups that a
user belongs to- just like the Unix groups command. Could someone
please help?

The "just like the unix groups command" sound makes this sound like a
self-answering question to me.

my @groups = split " ", `groups $user`;

Xho
 
J

John W. Krahn

I am working on a script that would run on Solaris and I need a Perl
script/function/module that would give me the list of groups that a
user belongs to- just like the Unix groups command. Could someone
please help?

my $user = 'tanveer';

my @groups = scalar getgrgid( ( getpwnam $user )[ 3 ] );

while ( my ( $name, $users ) = ( getgrent )[ 0, -1 ] ) {
$users =~ /\b$user\b/ and push @groups, $name
}

print "$user : @groups\n";




John
 
T

Ted Zlatanov

JM> This is not a group to ask for ready-to-use solutions.

Why not?

I like to know if a ready-made solution exists for many common Perl
tasks all the time, so I don't write them myself.

What's wrong is expecting people to write the solution for you, which
Arshad did not do. He clearly asked for a module/function/solution to
help him in the course of writing a script, to get the list of groups
for a user. Why does he have to attempt to write such a common task
himself, given that it's probably been done already a million times?

Let's be sensible. I understand the Posting Guidelines are important,
but they should not override common sense.

Ted
 
P

Peter J. Holzer

(e-mail address removed) wrote:
I am working on a script that would run on Solaris and I need a Perl
script/function/module that would give me the list of groups that a
user belongs to- just like the Unix groups command. Could someone
please help?
[...]
I haven't tried anything because I don't even know where to start.
Googl'ing for answers, I came across functions like getgrnam but they
give a list of users in a group. I want it the other way round. Given
a user id, how to determine the groups?

You mean: you don't know anything about Perl? Or you don't know anything
about where Solaris keeps this information?

One of the major features of Perl are its text processing abilities.
Usually Un*x base systems keep the information about users and groups in
text files in /etc (I would be surprised if Solaris did otherwise).

Solaris likes to use NIS. LDAP is also quite popular these days.
So the text file case may be less usual than you think.

However, regardless on how the information is actually stored, using the
getpw* and getgr* functions should always work. To find all groups a
user belongs to, you have to get the primary group with getpw(uid|nam)
and then loop over all groups returned by getgrent and check for each
group whether the user is a member. AFAIK there is no faster portable
way to do this.

hp
 
P

Peter J. Holzer

Read about the "getgrent" function:

Start->Run->"perldoc -f getgrent"

It's been some time that I've used Solaris, but I think the
"Start->Run->" part of your advice doesn't work there :).

hp
 
J

Josef Moellers

Ted said:
JM> This is not a group to ask for ready-to-use solutions.

Why not?

I like to know if a ready-made solution exists for many common Perl
tasks all the time, so I don't write them myself.

What's wrong is expecting people to write the solution for you, which
Arshad did not do. He clearly asked for a module/function/solution to
help him in the course of writing a script, to get the list of groups
for a user. Why does he have to attempt to write such a common task
himself, given that it's probably been done already a million times?

Let's be sensible. I understand the Posting Guidelines are important,
but they should not override common sense.

I'm afraid you're right: I have over-reacted a little and apologize to
Arshad.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top