加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

批量删除用户--Shell脚本

发布时间:2020-12-15 16:20:50 所属栏目:安全 来源:网络整理
导读:#!/bin/bash##User:Mobanche#Date:2017-7-17#Work:DeleteUser###############################################DefineFunction###############################################functionget_answer{#unsetANSWERASK_COUNT=0#while[-z"$ANSWER"]doASK_COUNT=$
#!/bin/bash
#
#User:Mobanche
#Date:2017-7-17
#Work:DeleteUser
#
#############################################
#DefineFunction#
#############################################
#
functionget_answer{
#
unsetANSWER
ASK_COUNT=0
#
while[-z"$ANSWER"]
do
ASK_COUNT=$[$ASK_COUNT+1]
#
case$ASK_COUNTin
2)
echo
echo"Pleaseanswerthequestion"
echo
;;
3)
echo
echo"Onelasttry...oleaseanswerthequestion"
echo
;;
4)
echo
echo"Sinceyourefusetoanswerthequestion...."
echo"exitingscripts"
echo
exit
;;
esac
#
echo
#
if[-n"$LINE2"]
then
echo$LINE1
echo-e$LINE2"c"
else
echo-e$LINE1"c"
fi
####################################################
#Allow60secondstoanswerbeforetime-out#
####################################################
read-t60ANSWER
done
###################cleanup#####################
unsetLINE1
unsetLINE2
}
#
functionprocess_answer{
#
case$ANSWERin
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
####ifuseranswer"yes",donothing######
;;
*)
#Ifuseranswersanythingbut"yes",exitscript#
echo
echo$EXIT_LINE1
echo$EXIT_LINE2
echo
exit
;;
esac
#
######################################################
#Doalittlevariableclean-up#
######################################################
unsetEXIT_LINE1
unsetEXIT_LINE2
#
}
###########################################################
#EndofFunctionDefinitions#
###########################################################
#
#
##########################################################
###################Mainscripts####################
##########################################################
#
echo"Step#1-DetermineUserAccountnametiDelete"
echo
LINE1="Pleaserentertheusernameoftheuser"
LINE2="Accountyouwashtodeletefromsystem:"
get_answer
USER_ACCOUNT=$ANSWER
#
#DoublecheckwithscriptuserthatthisistheacorrectUserAccount
#
LINE1="Is$USER_ACCOUNTtheuseraccount"
LINE2="Youwashtodeletefromthesystem?[y/n]"
get_answer
#
#Callprocess_answerfuntion:
#
#ifuseranswersanythingbut"yes",exitscript
#
EXIT_LINE1="Becasetheaccount,$USER_ACCOUNT,isnot"
EXIT_LINE2="Theoneyouwishtodelete,weareleavingthescripts"
process_answer
#
#########################################################################
#CheckthatUSER_ACCOUNTisreallyanaccountonthesystem
#
USER_ACCOUNT_RECORD=$(cat/etc/passwd|grep-w$USER_ACCOUNT)
#
if[$?-eq1]
then
echo
echo"Account,$USER_ACCOUNT.notfount."
echo"Leacingthescript....."
echo
exit
fi
#
echo
echo"Tfountthisrecord:"
echo$USER_ACCOUNT_RECORD
#
LINE1="IsthisthecorrectUserAccount?[y/n]"
get_answer
#
#
#Callprocess_answerfunction:
#Ifuseranswersanythingbut"yes",exitscript
#
EXIT_LINE1="Becausertheaccount,weareleavingthescript..."
process_answer
#
####################################################################
#SearchforanyrunningprocessesthatbelongtotheUserAccount
#
echo
echo"Step#2-Findprocessonsystembelongingtouseraccount"
echo
#
ps-u$USER_ACCOUNT>/dev/null
#
case$?in
1)
#NoprocessesrunningforthisUserAccount
echo"Therearenoprocessesforthisaccountcurrentlyrunning"
echo
;;
0)
#ProcessesrunningforthisUserAccount
#AskScriptUserifwantsustokilltheprocesses
#
echo"$USER_ACCOUNThasthefollowingprocessesrunning:"
echo
ps-u$USER_ACCOUNT
#
LINE1="Wouldyoulikemetokillthepeocess(es)?[y/n]"
get_answer
#
case$ANSWERin
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
echo
echo"Killingoffprocess(es)"
#
#Listuserprocessrunningcodeinvariable,COMMAND_1
COMMAND_1="ps-u$USER_ACCOUNT--no-heading"
#
#Createcommandtokillprocessinvariable,COMMAND_3
COMMAND_3="xargs-dn/usr/bin/sudo/bin/kill-9"
#
#Killprocessesviapipingcommandstogether
$COMMAND_1|gawk'{print$1}'|$COMMAND_3
#
echo
echo"Willnotkilltheprocess(es)"
echo
;;
*)
#Ifuseranswersany
echo
echo"Willnotkilltheprocess(es)"
echo
;;
esac
;;
esac
#
########################################################################
#CreateareportofallfilesownedbyUserAccount
#
echo
echo"Step#3-Findfilesonsystembelongingtouseraccount"
echo
echo"Creatingareportofallfilesownedby$USER_ACCOUNT"
echo
echo"Itisrecommendedthatyoubackup/archivethesefiles"
echo"andthendooneoftwothings;"
echo"1)Deletethefiles"
echo"2)Changethefilesownershiptoacurrentuseraccount"
echo
echo"Pleasewait,Thismaytakeawhile......"
#
REPORT_DATE=$(date+%y%m%d)
REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt"
#
find/-user$USER_ACCOUNT>$REPORT_FILE2>/dev/null
#
echo
echo"Reportiscomplete."
echo"Nameofreport:$REPORT_FILE"
echo"Locationofreport:$(pwd)"
echo
#
###############################################################
##
#############RemoveUserAccount###############
##
###############################################################
echo
echo"Step#4-Removeuseraccount"
echo
#
LINE1="Remove$USER_ACCOUNT'saccountfromsystem?[y/n]"
get_answer
#
#Callprocess_answerfunction:
#ifuseranswersanythingbut"yes",exitscript
#
EXIT_LINE1="Sinceyoudinotwishtoremovetheuseraccount"
EXIT_LINE2="$USER_ACCOUNTatthistime,exitingthescript..."
process_answer
#
#
userdel$USER_ACCOUNT|rm-rf/home/$USER_ACCOUNT
echo
echo"Useraccount,hasbeenremoved"
echo
#
exit

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读