自动化删除用户账户脚本:Delete_User.sh 详解
1. 引言
在系统管理中,删除用户账户是一项常见但需要谨慎处理的任务。为了简化这个过程并避免潜在的问题,我们可以使用一个自动化脚本。本文将详细介绍
Delete_User.sh
脚本,它可以帮助我们完成删除用户账户的一系列操作。
2. 脚本的主要步骤
该脚本主要包含以下四个步骤:
1. 获取并确认要删除的用户账户名称。
2. 查找并终止该用户账户的所有运行进程。
3. 创建该用户账户所有文件的报告。
4. 删除该用户账户。
下面是这些步骤的流程图:
graph TD;
A[获取并确认用户账户名称] --> B[查找并终止用户进程];
B --> C[创建用户文件报告];
C --> D[删除用户账户];
3. 脚本的函数定义
脚本中定义了两个重要的函数:
get_answer
和
process_answer
。
3.1
get_answer
函数
该函数用于获取用户的回答。如果用户在规定时间内没有给出回答,会进行多次提示,最多提示 4 次,若仍未回答则退出程序。
function get_answer {
unset answer
ask_count=0
while [ -z "$answer" ] #While no answer is given, keep asking.
do
ask_count=$[ $ask_count + 1 ]
case $ask_count in #If user gives no answer in time allotted
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 answer the question..."
echo "exiting program."
echo
exit
;;
esac
if [ -n "$line2" ]
then #Print 2 lines
echo $line1
echo -e $line2" \c"
else #Print 1 line
echo -e $line1" \c"
fi
read -t 60 answer
done
unset line1
unset line2
}
3.2
process_answer
函数
该函数用于处理用户的回答。如果用户回答“yes”,则继续执行;否则,输出提示信息并退出脚本。
function process_answer {
answer=$(echo $answer | cut -c1)
case $answer in
y|Y)
# If user answers "yes", do nothing.
;;
*)
# If user answers anything but "yes", exit script
echo
echo $exit_line1
echo $exit_line2
echo
exit
;;
esac
unset exit_line1
unset exit_line2
}
4. 具体步骤实现
4.1 获取并确认用户账户名称
首先,脚本会提示用户输入要删除的用户账户名称,并进行两次确认。
echo "Step #1 - Determine User Account name to Delete "
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 account, $user_account, is not "
exit_line2="the one you wish to delete, we are leaving the script..."
process_answer
4.2 检查用户账户是否存在
使用
grep
命令在
/etc/passwd
文件中查找该用户账户的记录。如果未找到,则输出提示信息并退出脚本;否则,再次确认该账户是否正确。
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
line1="Is this the correct User Account? [y/n]"
get_answer
exit_line1="Because the account, $user_account, is not "
exit_line2="the one you wish to delete, we are leaving the script..."
process_answer
4.3 查找并终止用户进程
使用
ps
命令查找该用户账户的运行进程。根据
ps
命令的退出状态,判断是否有进程在运行。如果有进程在运行,会询问用户是否要终止这些进程。
echo
echo "Step #2 - Find process on system belonging to user account"
echo
ps -u $user_account > /dev/null
case $? in
1)
echo "There are no processes for this account currently running."
echo
;;
0)
echo "$user_account has the following process(es) running:"
ps -u $user_account
line1="Would you like me to kill the process(es)? [y/n]"
get_answer
answer=$(echo $answer | cut -c1)
case $answer in
y|Y)
echo
echo "Killing off process(es)..."
command_1="ps -u $user_account --no-heading"
command_3="xargs -d \\n /usr/bin/sudo /bin/kill -9"
$command_1 | gawk '{print $1}' | $command_3
echo
echo "Process(es) killed."
;;
*)
echo
echo "Will not kill process(es)."
;;
esac
;;
esac
4.4 创建用户文件报告
使用
find
命令查找该用户账户的所有文件,并将结果保存到一个报告文件中。
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 recommended that you backup/archive these files,"
echo "and then do one of two things:"
echo " 1) Delete the files"
echo " 2) Change 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 -n "Location of report: "; pwd
echo
4.5 删除用户账户
在删除用户账户之前,会再次询问用户是否确认删除。如果用户确认,则使用
userdel
命令删除该用户账户。
echo
echo "Step #4 - Remove user account"
echo
line1="Do you wish to remove $user_account's account from 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 been removed"
echo
exit
5. 运行脚本
在运行脚本之前,需要给脚本文件添加执行权限:
chmod u+x Delete_User.sh
运行脚本时,需要以 root 用户身份或使用
sudo
命令:
sudo ./Delete_User.sh
下面是一个运行示例:
$ sudo ./Delete_User.sh
[sudo] password for christine:
Step #1 - Determine User Account name to Delete
Please enter the username of the user
account you wish to delete from system: consultant
Is consultant the user account
you wish to delete from the system? [y/n] yes
I found this record:
consultant:x:1003:1004::/home/consultant:/bin/bash
Is this the correct User Account? [y/n] y
Step #2 - Find process on system belonging to user account
consultant has the following process(es) running:
PID TTY TIME CMD
5781 ? 00:00:00 systemd
[…]
5884 ? 00:00:00 trojanhorse.sh
5885 ? 00:00:00 sleep
5886 ? 00:00:00 badjuju.sh
5887 ? 00:00:00 sleep
Would you like me to kill the process(es)? [y/n] y
Killing off process(es)...
Process(es) killed.
Step #3 - Find files on system belonging to user account
Creating a report of all files owned by consultant.
It is recommended that you backup/archive these files,
and then do one of two things:
1) Delete the files
2) Change the files' ownership to a current user account.
Please wait. This may take a while...
Report is complete.
Name of report: consultant_Files_200812.rpt
Location of report: /home/christine/scripts
Step #4 - Remove user account
Do you wish to remove consultant's account from system? [y/n] yes
User account, consultant, has been removed
$ ls *.rpt
consultant_Files_200812.rpt
$ grep ^consultant /etc/passwd
$
6. 总结
Delete_User.sh
脚本通过一系列步骤,帮助我们安全、便捷地删除用户账户。它不仅可以避免手动操作可能带来的错误,还可以提高工作效率。在使用该脚本时,需要注意以 root 用户身份或使用
sudo
命令运行,以确保有足够的权限进行操作。
自动化删除用户账户脚本:Delete_User.sh 详解
7. 脚本关键技术点分析
在
Delete_User.sh
脚本中,运用了多个关键技术来实现用户账户的删除操作,下面对这些技术进行详细分析。
7.1
grep
命令的使用
grep
命令用于在
/etc/passwd
文件中查找指定用户账户的记录。使用
-w
选项可以实现精确匹配,确保只找到完全符合用户账户名的记录。示例代码如下:
user_account_record=$(cat /etc/passwd | grep -w $user_account)
如果
grep
命令未找到匹配记录,其退出状态码为 1,脚本会根据这个状态码判断账户是否存在。
7.2
ps
命令的使用
ps
命令用于查找指定用户账户的运行进程。使用
-u
选项可以指定要查找的用户账户。示例代码如下:
ps -u $user_account > /dev/null
将命令输出重定向到
/dev/null
可以避免在没有进程时显示不必要的表头信息,防止用户产生混淆。根据
ps
命令的退出状态码,脚本可以判断是否有进程在运行。
7.3
find
命令的使用
find
命令用于查找指定用户账户的所有文件。使用
-user
选项可以指定要查找的用户账户,搜索范围为整个虚拟目录
/
。示例代码如下:
find / -user $user_account > $report_file 2>/dev/null
将命令输出重定向到报告文件
$report_file
中,并将错误输出重定向到
/dev/null
以忽略可能的错误信息。
7.4
xargs
命令的使用
xargs
命令用于从标准输入构建并执行命令。在脚本中,它用于根据
ps
和
gawk
命令输出的进程 ID(PID)来执行
kill
命令,终止用户账户的运行进程。示例代码如下:
command_3="xargs -d \\n /usr/bin/sudo /bin/kill -9"
使用
-d
选项指定分隔符为换行符
\n
,确保每个 PID 被视为一个单独的参数。由于
xargs
命令被赋值给变量,需要对
\n
中的反斜杠进行转义。
8. 操作步骤总结
为了更清晰地展示
Delete_User.sh
脚本的使用过程,下面将操作步骤总结成表格。
| 步骤 | 操作内容 | 代码示例 |
|---|---|---|
| 1 | 给脚本文件添加执行权限 |
chmod u+x Delete_User.sh
|
| 2 |
以 root 用户身份或使用
sudo
命令运行脚本
|
sudo ./Delete_User.sh
|
| 3 | 输入要删除的用户账户名称并确认 |
按照脚本提示输入账户名,回答
y
或
Y
确认
|
| 4 | 确认账户记录是否正确 |
查看账户记录,回答
y
或
Y
确认
|
| 5 | 处理用户进程 | 如果有进程在运行,选择是否终止进程 |
| 6 | 等待文件报告生成 | 脚本会自动生成报告文件 |
| 7 | 确认删除用户账户 |
回答
y
或
Y
确认删除
|
9. 流程图回顾与拓展
下面再次展示
Delete_User.sh
脚本的主要执行流程,同时对部分步骤进行拓展说明。
graph TD;
A[获取并确认用户账户名称] --> B[检查用户账户是否存在];
B -- 存在 --> C[查找并终止用户进程];
B -- 不存在 --> E[输出提示并退出脚本];
C -- 有进程 --> D1[询问是否终止进程];
C -- 无进程 --> D2[跳过进程终止步骤];
D1 -- 是 --> D3[终止进程];
D1 -- 否 --> D2;
D3 --> F[创建用户文件报告];
D2 --> F;
F --> G[确认删除用户账户];
G -- 是 --> H[删除用户账户];
G -- 否 --> I[输出提示并退出脚本];
10. 注意事项
在使用
Delete_User.sh
脚本时,需要注意以下几点:
-
权限问题
:由于脚本涉及到系统账户和文件的操作,必须以 root 用户身份或使用
sudo
命令运行,以确保有足够的权限进行操作。
-
文件备份
:在删除用户账户之前,建议先备份该用户账户的所有文件,避免数据丢失。
-
进程终止
:终止用户账户的运行进程可能会影响正在进行的工作,在操作前请确保用户已经保存好重要数据。
11. 总结与展望
Delete_User.sh
脚本通过一系列自动化步骤,帮助我们安全、便捷地删除用户账户。它不仅可以避免手动操作可能带来的错误,还可以提高工作效率。在未来的系统管理中,可以根据实际需求对脚本进行扩展,例如增加日志记录功能、支持批量删除用户账户等,以进一步提升脚本的实用性和灵活性。
通过对
Delete_User.sh
脚本的详细分析和使用,我们可以更好地掌握用户账户删除的操作流程和相关技术,为系统管理工作提供有力的支持。
超级会员免费看

被折叠的 条评论
为什么被折叠?



