return to PRS Technologies website
#!/usr/bin/ksh
###############################################################################
#
# Module: remove_users.sh.sh
# Author: Peter R. Schmidt
# Description: Script for removing old Unix users in a controlled and protected way
# Designed for HP/UX and Elite, but probably can be used for other systems
# Uses the Unix utility "userdel"
#
# Change Log
#
# Date Name Description.................
# 04/20/01 Peter R. Schmidt Start Program
#
###############################################################################
HOMEDIR=/elite/work/home
REMOVE_LIST=remove_list.txt
TMPDIR=/tmp
TARFILE=$TMPDIR/remove_users.tar
ZIPFILE=remove_users.tar.gz
if [ $# != 0 ]; then
RERUN=$1
else
RERUN=n
fi
cd $HOMEDIR
UDATE=`date +%y%m%d`
RECYCLE=RECYCLE_BIN.${UDATE}
LOG=$RECYCLE/remove_users.log
if [ ! -d $RECYCLE ]; then
mkdir $RECYCLE
echo "Created RECYCLE directory of: $RECYCLE" | tee -a $LOG
else
if [ $RERUN != y ]; then
rm -f $LOG
else
rm -f $TARFILE
rm -f $RECYCLE/$ZIPFILE
fi
fi
if [ $RERUN != y ]; then
date | tee -a $LOG
cp -p $REMOVE_LIST $RECYCLE
fi
echo
echo "Remove old users..."
echo
echo "Recycle bin is: $RECYCLE"
echo
echo "Checking..."
echo
if [ ! -f $REMOVE_LIST ]; then
echo "List of users to remove: $REMOVE_FILE does not exist" | tee -a $LOG
exit 1
fi
if [ $RERUN != y ]; then
for USER in `cat $REMOVE_LIST`
do
cnt1=`grep -c "^${USER}:" /etc/passwd`
if [ $cnt1 != 1 ];then
if [ ! -d $USER ]; then
echo | tee -a $LOG
echo "Warning: user: $USER is not in /etc/passwd AND does not have a home directory" | tee -a $LOG
echo "This suggests a error in your remove user list" | tee -a $LOG
echo | tee -a $LOG
else
echo "Warning: user: $USER is NOT IN /etc/passwd BUT HAS A HOME DIRECTORY." | tee -a $LOG
fi
continue
fi
USER_HOME=`egrep "^${USER}:" /etc/passwd | cut -d":" -f6`
if [ $USER_HOME != ${HOMEDIR}/${USER} ]; then
if [ -d $USER_HOME ]; then
echo "Warning: user: $USER has a NON-STANDARD Home directory of $USER_HOME." | tee -a $LOG
else
echo | tee -a $LOG
echo "Warning: user: $USER has a NON-STANDARD Home directory of $USER_HOME." | tee -a $LOG
echo " which doesn't even exist!!!" | tee -a $LOG
echo | tee -a $LOG
fi
continue
fi
if [ ! -d $USER ]; then
echo "Warning: user: $USER does not have a home directory under $HOMEDIR" | tee -a $LOG
fi
done
fi
################################################################################
echo
echo "Press to continue"
read answer
################################################################################
for USER in `cat $REMOVE_LIST`
do
cnt1=`grep -c "^${USER}:" /etc/passwd`
if [ $cnt1 = 1 ];then
USER_HOME=`egrep "^${USER}:" /etc/passwd | cut -d":" -f6`
if [ $USER_HOME != ${HOMEDIR}/${USER} ]; then
if [ -d $USER_HOME ]; then
echo "user: $USER has a NON-STANDARD Home directory of $USER_HOME which exists"
else
echo "user: $USER has a NON-STANDARD Home directory of $USER_HOME which does not exist."
fi
echo "no action"
continue
fi
fi
if [ ! -d $USER ]; then
echo "user: $USER does not have a home directory under $HOMEDIR"
echo "no action"
else
echo
echo "Move user's directory of $USER to $RECYCLE ? (y/n)"
read answer
if [ $answer = y -o $answer = Y ]; then
mv $USER $RECYCLE
echo "moved"
else
echo "not moved"
fi
fi
done
################################################################################
echo
echo "Done"
echo
echo "Press to compress"
read answer
################################################################################
echo "tar up old users files..."
cd $RECYCLE
tar cvf $TARFILE .
echo
ls -ls $TARFILE
echo
echo "zip up tar file..."
echo
gzip -9 -c $TARFILE > $ZIPFILE
echo
ls -ls $ZIPFILE
if [ -f $ZIPFILE -a -f $TARFILE ]; then
echo "Press to delete the temp TAR file"
read answer
rm -f $TARFILE
echo "done"
else
echo "Error"
exit 1
fi
################################################################################
echo
echo "Press to retire users files"
read answer
################################################################################
lsd
echo "Current directory is: "
pwd
echo
for USER in `cat $REMOVE_LIST`
do
echo
echo "Remove $USER ? (y/n)"
read answer
if [ $answer = y -o $answer = Y ]; then
if [ -d $USER ]; then
userdel -r $USER
if [ $? = 0 ]; then
echo "removed"
else
echo "Problem encountered: NOT removed"
fi
else
userdel $USER
if [ $? = 0 ]; then
echo "removed"
else
echo "Problem encountered: NOT removed"
fi
fi
else
echo "NOT removed"
fi
done
echo
echo "all done"
################################################################################