|
Written by Sam Agnew
|
|
Monday, 28 August 2006 |
|
This is a bash script that gives you the membership of any Open Directory group whether it contains other nested groups or not. In "debug" mode it gives you all information about whether a group contains other nested groups, what those groups are and what members are direct members rather than inherited.
Usage: ./grouplister.bash [-debug]
Example: ./grouplister.bash staff
For a list of all members of group with shortname of "staff"
Example: ./grouplister.bash staff -debug
For full information about group membership of group with shortname of "staff" and nested groups
This is a bash script that gives you the membership of any Open Directory group whether it contains other nested groups or not. In "debug" mode it gives you all information about whether a group contains other nested groups, what those groups are and what members are direct members rather than inherited.
Usage: ./grouplister.bash [-debug]
Example: ./grouplister.bash staff
For a list of all members of group with shortname of "staff"
Example: ./grouplister.bash staff -debug
For full information about group membership of group with shortname of "staff" and nested groups
Click here to download script.
#!/bin/bash
# List the members of an OD group whether nested or not
# Sam Agnew
# First version 2006-07-10
######
## User Input ##
group="$1" #Group shortname
debug="$2" #"-debug"
## ## ##
## Usage instructions ##
if [ "$group" == "" ]
then
# Usage
echo ""
echo 1>&2 "Usage: $0 <group shortname> [-debug]"
echo ""
echo "Example:
$0 staff"
echo "For a list of all members of group with shortname of \"staff\""
echo ""
echo "Example:
$0 staff -debug"
echo "For full information about group membership of group with shortname of \"staff\" and
nested groups"
echo ""
exit 127
fi
## ## ##
## Check if group exists ##
exists="$(dscl /Search -list /Groups | grep
-x $group)"
if [ "$exists" == "" ]
then
# No such group
echo ""
echo "The group \"$group\" does
not seem to exist in any directory you are attached to"
echo "Please
check your typing and/or the domain membership of this Mac and try again"
echo ""
exit 128
fi
## ## ##
## Figuring out the membership ##
nested="$(dscl /Search -read /Groups/"$group" |
grep NestedGroups)"
GroupMembership="$(dscl /Search -read /Groups/"$group" |
grep GroupMembership)"
subgroups="$(dscl /Search -read /Groups/"$group" NestedGroups
| sed 's|NestedGroups: ||g')"
containedGroups="$(for sub in $subgroups; do dscl
/Search -search /Groups GeneratedUID $sub | uniq | awk ' { print $1 } ' ; done)"
nestedMembers="$(for subMem in $containedGroups;
do dscl /Search -read /Groups/"$subMem" GroupMembership
| sed 's|GroupMembership: ||g' ; done)"
nM="$(for user in $nestedMembers; do echo $user;
done)"
allNestedMembers="$(echo "$nM" |
sort - | uniq)"
stdMembers="$(dscl /Search -read /Groups/"$group" GroupMembership
| sed 's|GroupMembership: ||g' | sort | uniq)"
sM="$(for user in $stdMembers; do echo $user; done)"
allStdMembers="$(echo "$sM" |
sort - | uniq)"
if [ "$GroupMembership" == "" ]
then
ttlMembers="$(echo
$allNestedMembers | sort | uniq)"
else
ttlMembers="$(echo
$allNestedMembers " " $stdMembers | sort
| uniq)"
fi
tM="$(for user in $ttlMembers; do echo $user; done)"
allTtlMembers="$(echo "$tM" |
sort - | uniq)"
## ## ##
## Output for "-debug" mode ##
if [ "$debug" == "-debug" ]
then
echo ""
echo "Group $group has long name of: $(dscl
/Search -read /Groups/$group RealName | sed 's|RealName: ||g')"
echo ""
echo "Group $group has Unix GID of: $(dscl
/Search -read /Groups/$group PrimaryGroupID | sed 's|PrimaryGroupID: ||g')"
echo ""
if [ "$nested" == "" ]
then
echo "Group
$group does NOT contain nested groups"
else
echo "Group
$group HAS nested groups"
echo "The
groups contained are:"
echo "$containedGroups"
echo
echo "Combined
membership of nested groups is:"
echo $allNestedMembers
echo ""
fi
if [ "$GroupMembership" == "" ]
then
echo "Group
$group does NOT contain standard members"
else
echo "Group
$group HAS standard members"
echo "Standard
membership is:"
echo $allStdMembers
echo ""
fi
echo "SUMMARY:"
echo ""
echo "Combined
total membership for group $group including nested groups and standard members
is:"
echo $allTtlMembers
echo ""
echo "Program
complete"
## ## ##
## Standard output ##
else
echo "$allTtlMembers"
fi
## ## ##
exit 0 |
|
Last Updated ( Monday, 30 October 2006 )
|