ntubski |
10-15-2006 09:55 AM |
I'm sorry, somehow I missed your earlier post.
Not sure what you mean by cutting of the "Shadow" file? The script looks at /etc/passwd and /etc/group not /etc/shadow :scratch:
I did find 2 problems with the script, I also added a --dry-run mode, which only prints out the commands that are going to be run without doing anything. Here is the updated version with changes in bold red:
Code:
#!/bin/bash
#
# delgroupmembers
# Shell script to delete all users in a specific group and delete the group.
#
# Written by Charles Rutledge
# Copyright (c) 2006 Centauri Computer Works, Inc.
#
# This program is released under the terms of the GNU General Public License,
# Version 2 (or at your option, any later version) as published by the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110.
# A copy of this license is available at http://www.gnu.org/copyleft/gpl.html.
#
# THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
# EXPRESSED OR IMPLIED. YOU ASSUME ALL RISKS FOR USE OF THIS PROGRAM.
ETC=/etc #for testing purposes, change to use different passwd and group files
DRY_RUN=false
# Be sure this program is run as root.
if [ $UID -ne 0 ]; then
echo "`basename $0` can only be run as root"
exit 1
fi
# We are expecting one or two arguments.
if [ -z "$1"; then
echo "USAGE: `basename $0` [--dry-run] <group>"
exit 1
elif [ "$1" = '--dry-run' ];then
if [ -z "$2" ];then
echo "USAGE: `basename $0` [--dry-run] <group>"
exit 1
else
DRY_RUN=true
shift
fi
fi
# Be sure this is a valid group.
if [ `grep -c ^$1: $ETC/group` -eq 0 ]; then
echo "ERROR: Group $1 not found in $ETC/group"
exit 1
fi
# Find out the group number for given group
GROUPNUM=$(grep ^$1: $ETC/group | cut -f3 -d:)
# Make sure we found a number
if [ $( echo $GROUPNUM | egrep -c ^[[:digit:]]+$ ) -eq 0 ]; then
echo "ERROR: Couldn't find group number for group $1"
exit 1
fi
# Find all users with this primary group according to /etc/passwd
USERLIST=$(egrep "^(.*):x:[[:digit:]]+:$GROUPNUM:.*:/home/$1/\1:.*$" $ETC/passwd | cut -f1 -d:)
# Then delete the users and group
if [ -z "$USERLIST" ]; then
echo "ERROR: There were no users found for group $1"
exit 1
fi
if [ $DRY_RUN = true ]; then
echo "DRY RUN MODE"
for user in $USERLIST ; do
echo "userdel -r $user"
echo "rm -r /home/$1/$user"
echo "smbpasswd -x $user"
done
echo "groupdel $1"
else
for user in $USERLIST ; do
userdel -r $user
rm -r /home/$1/$user
smbpasswd -x $user
done
groupdel $1
fi
If it still doesn't work post the output when you run bash -x /sbin/delgroup nhs2001
|