linux howto get user's group,unix - How to list all users in a Linux group? - Stack Overflow

Zed's implementation should probably be expanded to work on some of the other major UNIX.

Someone have access to Solaris or HP-UX hardware?; did not test those cases.

#!/usr/bin/perl

#

# Lists members of all groups, or optionally just the group

# specified on the command line

#

# Date: 12/30/2013

# Author: William H. McCloskey, Jr.

# Changes: Added logic to detect host type & tailor subset of getent (OSX)

# Attribution:

# The logic for this script was directly lifted from Zed Pobre's work.

# See below for Copyright notice.

# The idea to use dscl to emulate a subset of the now defunct getent on OSX

# came from

# http://zzamboni.org/\

# brt/2008/01/21/how-to-emulate-unix-getent-with-macosxs-dscl/

# with an example implementation lifted from

# https://github.com/petere/getent-osx/blob/master/getent

#

# Copyright © 2010-2013 by Zed Pobre (zed@debian.org or zed@resonant.org)

#

# Permission to use, copy, modify, and/or distribute this software for any

# purpose with or without fee is hereby granted, provided that the above

# copyright notice and this permission notice appear in all copies.

#

use strict; use warnings;

$ENV{"PATH"} = "/usr/bin:/bin";

# Only run on supported $os:

my $os;

($os)=(`uname -a` =~ /^([\w-]+)/);

unless ($os =~ /(HU-UX|SunOS|Linux|Darwin)/)

{die "\$getent or equiv. does not exist: Cannot run on $os\n";}

my $wantedgroup = shift;

my %groupmembers;

my @users;

# Acquire the list of @users based on what is available on this OS:

if ($os =~ /(SunOS|Linux|HP-UX)/) {

#HP-UX & Solaris assumed to be like Linux; they have not been tested.

my $usertext = `getent passwd`;

@users = $usertext =~ /^([a-zA-Z0-9_-]+):/gm;

};

if ($os =~ /Darwin/) {

@users = `dscl . -ls /Users`;

chop @users;

}

# Now just do what Zed did - thanks Zed.

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 ",$group," has the following members:\n";

print_group_members($group);

print "\n";

}

}

sub print_group_members

{

my ($group) = @_;

return unless $group;

foreach my $member (sort keys %{$groupmembers{$group}})

{

print $member,"\n";

}

}

If there is a better way to share this suggestion, please let me know; I considered many ways, and this is what I came up with.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值