天天看点

管理用户账户 shell脚本

           最近又重温习了布鲁姆的<Linux命令行与Shell脚本编程大全>,相对于一个菜鸟入门来说,给了极大的帮助。本书尾给出了Shell脚本现实中的使用实例。

  • 该脚本目的是删除用户账户,节约系统管理员时间。首先创建一个用来测试的用户账号,useradd -m test         -m 选项强制创建home下的test目录,会将/etc/skel下的文件复制到test目录下,同时 可以通过chsh -s /bin/bash test指定 该用户的默认登录shell.  通过cat /etc/passwd | more 来查看test用户是否创建成功。
    管理用户账户 shell脚本
    管理用户账户 shell脚本
  • 删除用户分为4步
  1. 获得要删除用户的正确账户名。
  2. 强制终止正在系统上运行的属于该账户的进程
  3. 将属于该账户的文件记录在报告文件中,以便删除或更改所属
  4. 删除用户账户
  • USER_ACCOUNT_RECORD=$( cat /etc/passwd  | grep -w $USER_ACCOUNT )

                 在口令文件中匹配该账户,若有,则退出状态码为$?=0  否则$?=1

  •   ps -u $USER_ACCOUNT

                 查找是否有属于该账户的正在运行的进程,若有,则退出状态码为$?=0  否则$?=1

  •   ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt

                 将账户进程信息记录在报告中,便于while read 单个进程信息

  • exec  <  $USER_ACCOUNT_Running_Process.rpt

                  重定向输入,但为什么在重定向后 未曾恢复  便还能从键盘读取,搞不懂,正在研究

  •  USER_PID=$( echo $USER_ACCOUNT_REC | cut   -d  " " -f1 )

                从进程的状态记录中提取进程的id,然后才能kill -9 发送终止信号无条件终止进程

  • trap " rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT

               当信号被进程捕捉到时 执行  删除报告 命令

  •   find  /  -user   $USER_ACCOUNT  > $REPORT_FILE

             查找整个文件系统 将属于该账户的文件目录路径存放于 $REPORT_FILE中,便于删除和更改所属

      源代码如下:

#!/bin/bash
#
#Delete User 4steps
#
########################
#first,get the current username
########################
 get_answer()
{
  unset ANSWER
  ASK_COUNT=0


  while [ -z "$ANSWER" ]
	  do
		  ASK_COUNT=$[ $ASK_COUNT + 1 ]
		  case $ASK_COUNT in
		  2)
		     echo
			 echo "Please answer the question"
			 echo 
			 ;;
		  3)
		     echo
			 echo "One last try...please answer the question"
			 echo
			 ;;
		  4)
		     echo
			 echo "since you refuse to answerthe question"
			 echo "exiting program."
			 echo
			 exit
			 ;;
		  esac


		  echo


		  if [ -n "$LINE2" ] ; then
			  echo $LINE1
			  echo -e  $LINE2" \c"
			 else
			   echo -e $LINE1" \c"
           fi
           read -t 60 ANSWER
    done
#Do a little varible clean-up
unset LINE1
unset LINE2
}    #End of get_answer function
######################################
 process_answer ()
{
   case $ANSWER in
   y|Y|yes|YES|Yes|yeS|yES)
       ;;   #if answer is yes,do nothing
    *)
	  echo
	  echo $EXIT_LINE1
	  echo -n $EXIT_LINE2
	  echo
	  exit
	  ;;
	  esac
#Do a little varible clean-up
unset EXIT_LINE1
unset EXIT_LINE2
}    #End of process_answer function


##############################
echo "Step #1 -Determine User Account to Deletd"
echo
LINE1="Please enter the username of the user"
LINE2="account you wish to delete from system:"
  get_answer
  USER_ACCOUNT=$ANSWER
#
LINE1="Is $USER_ACCOUNT  the user account" 
LINE2="you wish to delete from the system [y/n]"
  get_answer
#
EXIT_LINE1="Because the count .$USER_ACCOUNT. is not"
EXIT_LINE2="The one you wish to delete .we  are leaving the script..."
 process_answer
#####################################
#Check the USER_ACCOUNT is really an account on the system
#
USER_ACCOUNT_RECORD=` cat /etc/passwd | grep -w $USER_ACCOUNT `
 if [ $? -eq 1 ] ; then
	 echo
	 echo "Account. $USER_ACCOUNT . not found."
	 echo "leaving the script"
     echo
	 exit
  fi
#
  echo
  echo "I found this record"
  echo  "$USER_ACCOUNT_RECORD"
  echo
# make sure again
LINE1="Is this the correct User Account? [y/n]"
  get_answer
#
#
EXIT_LINE1="Because the count .$USER_ACCOUNT. is not"
EXIT_LINE2="The one you wish to delete .we are leaving the script..."
process_answer
####################################################
#Search for any running process that belong to the User Account
#
    echo
    echo " Step 2. Find processes that belong to the User Account"
	echo
    echo " $USER_ACCOUNT has the following processes running: "
	echo
#
 ps -u $USER_ACCOUNT
     case $? in
	 1)     # No processes running for this User Account
      echo "There are no processes for this account Currently running"
	  echo
	  ;;
	 0)   
	 LINE1="Would you like me to kill process(es)? [y/n]"
	 get_answer
#
	 case $ANSWER in
	 y|Y|yes|YES|yeS|YEs|yES)    # kill User Account processes
        echo
# clear-up temp file upon signals
		trap " rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT
#
		ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt
#
	exec < $USER_ACCOUNT_Running_Process.rpt
#
	read USER_PROCESS_REC   #First record will be a blank
	read USER_PROCESS_REC
	while [ $? -eq 0 ]
		do
			USER_PID=` echo $USER_PROCESS_REC | cut -d " " -f1 `
            kill -9 $USER_PID
			echo "killed process $USER_PID"
			read USER_PROCESS_REC
	    done
		echo
		rm $USER_ACCOUNT_Running_Process.rpt  # Remove temp report
		;;
		*)   # do not kill
        echo
		echo " Will not killl process(es)"
		echo
		;;
		esac
     ;;
	 esac
#########################################################
#
	 echo
	 echo "Step #3 - Find files on system belonging to user account"
	 echo
	 echo "Creating a report of all files owned by $USER_ACCOUNT"
	 echo
	 echo "It is commended that you backup/archive there files"
	 echo "and then do one of two things"
	 echo "  1)Delete the files"
	 echo "  2)Changes the files'ownership to a current user account."
	 echo
	 echo "Please wait. this may take a while..."
#
	 REPORT_DATE=`date +%y%m%d`
	 REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt"
#
	 find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null
#
	 echo
     echo "Report is complete"
	 echo "Name of report:      $REPORT_FILE"
	 echo "Location of report :   `pwd` "
	 echo
###########################################################
#Remove User Account
	 echo
	 echo "Step #4 - Remove user account"
	 echo
#
	 LINE1="Do you wish to remove $USER_ACCOUNT's account fron system? [y/n]"
	 get_answer
#
	 EXIT_LINE1="Since you do not wish to remove the user account"
	 EXIT_LINE2="$USER_ACCOUNT at this time .exiting the script..."
	 process_answer
#
	 userdel $USER_ACCOUNT
	 echo
	 echo "User account. $USER_ACCOUNT.has benn removed"
	 echo
#
           
管理用户账户 shell脚本
管理用户账户 shell脚本
管理用户账户 shell脚本
  • 最终生成的报告文件
管理用户账户 shell脚本

继续阅读