Getting the list of groups given a user id

Discussion in 'Perl Misc' started by arshad.tanveer@gmail.com, Mar 15, 2007.

  1. Guest

    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
     
    , Mar 15, 2007
    #1
    1. Advertising

  2. wrote:
    > 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?

    --
    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
     
    Josef Moellers, Mar 15, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 15, 6:54 pm, Josef Moellers <josef.moell...@fujitsu-
    siemens.com> wrote:
    > wrote:
    > > 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?
    >
    > --
    > 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
     
    , Mar 15, 2007
    #3
  4. wrote:
    > On Mar 15, 6:54 pm, Josef Moellers <josef.moell...@fujitsu-
    > siemens.com> wrote:
    >
    >> wrote:
    >>
    >>>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?


    >
    > 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.
    --
    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
     
    Josef Moellers, Mar 15, 2007
    #4
  5. Mumia W. Guest

    On 03/15/2007 08:47 AM, wrote:
    > 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"

    -------
    Posting Guidelines for comp.lang.perl.misc:
    http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    Mumia W., Mar 15, 2007
    #5
  6. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 15, 2007
    #6
  7. 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?


    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
    --
    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
     
    John W. Krahn, Mar 15, 2007
    #7
  8. Ted Zlatanov Guest

    On Thu, 15 Mar 2007 15:35:45 +0100 Josef Moellers <> wrote:

    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
     
    Ted Zlatanov, Mar 15, 2007
    #8
  9. On 2007-03-15 14:35, Josef Moellers <> wrote:
    > wrote:
    >> On Mar 15, 6:54 pm, Josef Moellers <josef.moell...@fujitsu-
    >> siemens.com> wrote:
    >>> 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


    --
    _ | Peter J. Holzer | Blaming Perl for the inability of programmers
    |_|_) | Sysadmin WSR | to write clearly is like blaming English for
    | | | | the circumlocutions of bureaucrats.
    __/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
     
    Peter J. Holzer, Mar 15, 2007
    #9
  10. On 2007-03-15 14:40, Mumia W. <> wrote:
    > On 03/15/2007 08:47 AM, 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?

    >
    > 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


    --
    _ | Peter J. Holzer | Blaming Perl for the inability of programmers
    |_|_) | Sysadmin WSR | to write clearly is like blaming English for
    | | | | the circumlocutions of bureaucrats.
    __/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
     
    Peter J. Holzer, Mar 15, 2007
    #10
  11. Ted Zlatanov wrote:
    > On Thu, 15 Mar 2007 15:35:45 +0100 Josef Moellers <> wrote:
    >
    > 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.

    --
    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
     
    Josef Moellers, Mar 16, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anonymous
    Replies:
    1
    Views:
    4,657
    Francisco Padron
    May 8, 2005
  2. Lord0
    Replies:
    1
    Views:
    599
    Thomas Weidenfeller
    Apr 19, 2006
  3. chiara
    Replies:
    6
    Views:
    501
    Barry Schwarz
    Oct 6, 2005
  4. 2Barter.net
    Replies:
    0
    Views:
    395
    2Barter.net
    Dec 13, 2006
  5. Casey Hawthorne
    Replies:
    385
    Views:
    6,032
    ng2010
    Apr 4, 2010
Loading...

Share This Page