• manage account


      1 #!/bin/bash
      2 #
      3 #Delete_user - Automates the 4 steps to remove an account
      4 #
      5 ###############################################################
      6 #Define Functions
      7 #
      8 ###############################################################
      9 function get_answer {
     10 #
     11 unset ANSWER
     12 ASK_COUNT=0
     13 #
     14 while [ -z "$ANSWER" ]    #While no answer is given,keeping asking......
     15 do
     16 ASK_COUNT=$[ $ASK_COUNT + 1 ]
     17 #
     18 case $ASK_COUNT in #If user gives no answer in time allotted
     19 2)
     20 echo
     21 echo "Please answer the question."
     22 echo
     23 ;;
     24 3)
     25 echo 
     26 echo "One last try...please answer the question."
     27 echo
     28 ;;
     29 4)
     30 echo 
     31 echo "Since you refuse to answer the question..."
     32 echo "exiting program."
     33 #
     34 exit
     35 ;;
     36 esac
     37 #
     38 echo
     39 #
     40 if [ -n "$LINE2" ]
     41 then #Print 2 lines
     42 echo "$LINE1"
     43 echo -e $LINE2"c"    # "-e" Deal with paticualar character,'c' is oppsite with '
    '.
     44 else    #print 1 line
     45 echo -e $LINE1"c"
     46 fi
     47 #
     48 #    Allow 60 seconds to answer before time-out
     49 read -t 60 ANSWER
     50 done
     51 #Do a little variable clean-up
     52 unset LINE1
     53 unset LINE2
     54 #
     55 }    #End of get_answer function.
     56 #
     57 ##########################################################################
     58 function process_answer {
     59 #
     60 unset ANSWER
     61 echo "process_answer[y/n]?"
     62 read -t 30 ANSWER
     63 case $ANSWER in
     64 y|Y|YES|yes|yEs|yeS|YEs|yES|Yes|YeS)
     65 #If user answer "yes" ,do nothing.
     66 ;;
     67 *)
     68 #If user answers anything but "yes",exit script
     69 echo 
     70 echo $EXIT_LINE1
     71 echo $EXIT_LINE2    
     72 echo 
     73 exit
     74 ;;
     75 esac
     76 #
     77 #Do a little variable clean-up
     78 #
     79 unset EXIT_LINE1
     80 unset EXIT_LINE2
     81 #
     82 }    ##End of process_answer function
     83 #
     84 #
     85 #########################################################################
     86 #End of Function Definitions    #
     87 #    #
     88 #########################################################################
     89 #    #
     90 #    #
     91 ############# Mani Scripts ##############################################
     92 #    #
     93 #    #
     94 #    #
     95 #########################################################################
     96 #
     97 ##Get name of User Accout to check
     98 #
     99 echo "Step #1 - Determine User Accout name to Delete "
    100 echo 
    101 LINE1="Please enter the username of the user"
    102 LINE2="account you wish to delete from system"
    103 get_answer
    104 USER_ACCOUNT=$ANSWER
    105 #
    106 #Call process_answer function
    107 #    if user answers anything but "yes" ,exit script
    108 #
    109 EXIT_LINE1="Because the account,$USER_ACCOUNT is not"
    110 EXIT_LINE2="the one you wish to delete,we are leaving the script..."
    111 process_answer
    112 #
    113 ############################################################################
    114 #Check that USER_ACCOUNT is really an account on the system
    115 #
    116 USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)
    117 #
    118 if [ $? -eq 1 ] #If the account is not found,exit script.
    119 then 
    120 echo
    121 echo "Account ,$USER_ACOUNT,not found."
    122 echo "leaving the script..."
    123 echo
    124 exit
    125 fi
    126 #
    127 echo 
    128 echo "I found this record:"
    129 echo $USER_ACCOUNT_RECORD
    130 echo
    131 #
    132 LINE1="Is this the correct User Account?[y/n]"
    133 get_answer
    134 #
    135 #
    136 #Call process_answer function:
    137 #    if user answer anything but "yes" .exit script
    138 #
    139 EXIT_LINE1="Because the account ,$USER_ACCOUNT,is not"
    140 EXIT_LINE2="the one you wish to delete .we are leaving the script..."
    141 process_answer
    142 #
    143 ##############################################################################
    144 #Search for any running processes that belong to the use account
    145 #
    146 echo 
    147 echo "Step #2 - Find process on system belonging to user Account"
    148 echo 
    149 echo "$USER_ACCOUNT has the following processes running:"
    150 echo
    151 #
    152 ps -u $USER_ACCOUNT #List user processes running
    153 case $? in
    154 1)    #No processes running for this user account
    155 #
    156 echo "There are no processes for this account currently running."
    157 echo
    158 ;;
    159 0)    #processes running for this User Account.
    160 #Ask Script User if wants us to kill the processes.
    161 #
    162 unset ANSWER
    163 LINE1="Would you like me to kill the processes(es)?[y/n]"
    164 get_answer
    165 #
    166 case $ANSWER in
    167 y|Y|yes|Yes|yEs|yeS|YEs|YeS|yES|YES)    #if user answer 'yes'.
    168 #
    169 echo
    170 #
    171 #Clean-up temp file upon signals
    172 trap "rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT
    173 #
    174 #List user processes running
    175 ps -u $USER_ACCOUNT_Running_Process.rpt
    176 #
    177 exec < $USER_ACCOUNT_Runnning_Process.rpt    #make report Std Input
    178 #
    179 read USER_PROCESS_REC    #First record will be blank
    180 read USER_PROCESS_REC
    181 #
    182 while [ $? -eq 0 ]
    183 do
    184 #obtain PID
    185 USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1)    # output a line of characters,'cut -d "," -f2' will select second 
    186 # part which splited by ",".Is same to" gawk '{print $2}' ".
    187 kill -9 $USER_PID
    188 echo "Killed process $USER_PID"
    189 read USER_PROCESS_REC
    190 done
    191 #
    192 echo
    193 rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report
    194 ;;
    195 *)    #If user answers anything but "yes",do not kill
    196 echo 
    197 echo "Will not kill the process(es)."
    198 echo
    199 ;;
    200 esac
    201 ;;
    202 esac
    203 #########################################################################################
    204 # Create a report of all files owned by User Account
    205 #
    206 echo
    207 echo "step #3 - Find files on system belonging to user account"
    208 echo 
    209 echo "Creating a report of all files owned by $USER_ACCOUNT."
    210 echo 
    211 echo "It is recommended that you backup/archive these files."
    212 echo "and then do one of two things:"
    213 echo " 1) Delete the files"
    214 echo " 2) Change the files' ownership to a current user account."
    215 echo 
    216 echo "Please wait .This may take a while..."
    217 #
    218 REPORT_DATE=`date +%y%m%d`
    219 REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt"
    220 #
    221 #
    222 find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null    # How to use "find"
    223 #    #There don't deal with $USER's file.
    224 echo
    225 echo "Report is conplete"
    226 echo "Namp of report:    $REPORT_FILE"
    227 echo "Location of report:    `pwd`"
    228 echo
    229 ############################################################################################
    230 # Remove User Account 
    231 echo
    232 echo "Step #4 - Romove user account"
    233 echo
    234 LINE1="Do you wish to remove $User_Account's account from system?[y/n]"
    235 get_answer
    236 #
    237 # Call process_answer function:
    238 #    if user answer anything but "yes" ,exit script
    239 #
    240 EXIT_LINE1="Since you do not wish to remove the user account."
    241 EXIT_LINE2="$USER_ACCOUNT at this time ,exiting the script..."
    242 process_answer
    243 #
    244 userdel $USER_ACCOUNT    #delete user account
    245 echo 
    246 echo "User account $USER_ACCOUNT has been removed"
    247 echo 
    248 #
    249 #
    250 #END
    251 
    252  
  • 相关阅读:
    UVA657 The die is cast(DFS、BFS)
    三分
    【洛谷P6105】iepsmCmq
    【CF613D】Kingdom and its Cities
    【洛谷P4294】游览计划
    【洛谷P3500】TESIntelligence Test
    【洛谷P6189】[NOI Online 入门组] 跑步
    【洛谷P2973】Driving Out the Piggies
    【洛谷P3164】和谐矩阵
    【洛谷P4161】游戏
  • 原文地址:https://www.cnblogs.com/little-snake/p/4555788.html
Copyright © 2020-2023  润新知