Last active
September 1, 2015 14:31
-
-
Save ype/58682bacd6c6d52fdf06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### -*- Mode: Perl -*- ### | |
| ################################################################################ | |
| ################################################################################ | |
| ### ### | |
| ### usrgrp ### | |
| ### Created: 01-09-2015 ### | |
| ### Author: Anton Strilchuk <[email protected]> ### | |
| ### URL: https://env.sh ### | |
| ### Version: 0.0.0.1 ### | |
| ### Last-Updated: 01-09-2015 ### | |
| ### Update #: 8 ### | |
| ### By: Anton Strilchuk <[email protected]> ### | |
| ### ### | |
| ### Description: ### | |
| ### Lists all groups and users belonging to them ### | |
| ### ### | |
| ### Usage: ### | |
| ### cpan install Term::ANSIColor ### | |
| ### ./usrgrp ### | |
| ### ### | |
| ################################################################################ | |
| ################################################################################ | |
| ## | |
| ## Code: | |
| #!/usr/bin/perl -T | |
| use Term::ANSIColor; | |
| use strict; use warnings; | |
| $ENV{"PATH"} = "/usr/bin:/bin"; | |
| my $wantedgroup = shift; | |
| my %groupmembers; | |
| my $usertext = `getent passwd`; | |
| my @users = $usertext =~ /^([a-zA-Z0-9_-]+):/gm; | |
| foreach my $userid (@users) | |
| { | |
| my $usergrouptext = `id -Gn $userid`; | |
| my @grouplist = split(' ',$usergrouptext); | |
| foreach my $group (@grouplist) | |
| { | |
| $groupmembers{$group}->{$userid} = 1; | |
| } | |
| } | |
| if($wantedgroup) | |
| { | |
| print_group_members($wantedgroup); | |
| } | |
| else | |
| { | |
| foreach my $group (sort keys %groupmembers) | |
| { | |
| print "group: "; | |
| print color('bold blue'); | |
| printf "%-40s", $group; | |
| print color('reset'); | |
| printf "%10s", "members: "; | |
| print_group_members($group); | |
| print "\n"; | |
| } | |
| } | |
| sub print_group_members | |
| { | |
| my ($group) = @_; | |
| return unless $group; | |
| foreach my $member (sort keys %{$groupmembers{$group}}) | |
| { | |
| print color('magenta'); | |
| print "\"",$member,"\" "; | |
| print color('reset'); | |
| } | |
| } | |
| ################################################################################ | |
| ## usrgrp ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment