196 lines
6.6 KiB
Bash
196 lines
6.6 KiB
Bash
#!/usr/bin/bash
|
||
|
||
# 定义彩色输出函数
|
||
color_echo() {
|
||
case $1 in
|
||
"red")
|
||
echo -e "\033[31m$2\033[0m"
|
||
;;
|
||
"green")
|
||
echo -e "\033[32m$2\033[0m"
|
||
;;
|
||
"yellow")
|
||
echo -e "\033[33m$2\033[0m"
|
||
;;
|
||
"blue")
|
||
echo -e "\033[34m$2\033[0m"
|
||
;;
|
||
"purple")
|
||
echo -e "\033[35m$2\033[0m"
|
||
;;
|
||
"cyan")
|
||
echo -e "\033[36m$2\033[0m"
|
||
;;
|
||
"white")
|
||
echo -e "\033[37m$2\033[0m"
|
||
;;
|
||
*)
|
||
echo "$2"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
# 输出菜单
|
||
list() {
|
||
color_echo "cyan" "--------------------------------------"
|
||
color_echo "cyan" "------------性能优化工具箱------------"
|
||
color_echo "cyan" "--------------------------------------"
|
||
color_echo "green" "1. 更换 YUM 源"
|
||
color_echo "green" "2. 关闭防火墙"
|
||
color_echo "green" "3. 时间同步"
|
||
color_echo "green" "4. 创建用户"
|
||
color_echo "green" "5. 删除用户"
|
||
color_echo "green" "6. 安装常用软件"
|
||
color_echo "green" "7. 内存使用情况"
|
||
color_echo "green" "8. 磁盘使用情况"
|
||
color_echo "green" "9. 服务器信息"
|
||
color_echo "green" "10 CPU使用情况"
|
||
color_echo "green" "11. 退出"
|
||
color_echo "cyan" "--------------------------------------"
|
||
}
|
||
|
||
# 更换 YUM 源
|
||
yuan() {
|
||
color_echo "yellow" "正在更换 YUM 源..."
|
||
rm -rf /etc/yum.repos.d/*
|
||
if curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null && \
|
||
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null; then
|
||
yum clean all
|
||
yum makecache fast
|
||
color_echo "green" "YUM 源更换完成!"
|
||
else
|
||
color_echo "red" "YUM 源更换失败,请检查网络或链接。"
|
||
fi
|
||
}
|
||
|
||
# 关闭防火墙
|
||
fire() {
|
||
color_echo "yellow" "正在关闭防火墙..."
|
||
systemctl stop firewalld
|
||
systemctl disable firewalld
|
||
setenforce 0 # 临时关闭 SELinux
|
||
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 永久关闭 SELinux
|
||
color_echo "green" "防火墙已关闭,SELinux 已禁用(需重启生效)。"
|
||
}
|
||
|
||
# 时间同步
|
||
timed() {
|
||
color_echo "yellow" "正在安装和同步时间..."
|
||
if yum -y install ntpdate &>/dev/null; then
|
||
ntpdate ntp1.aliyun.com
|
||
color_echo "green" "时间同步成功!"
|
||
else
|
||
color_echo "red" "时间同步失败,请检查网络连接或 YUM 配置。"
|
||
fi
|
||
}
|
||
|
||
# 创建用户
|
||
user() {
|
||
read -p "$(color_echo "yellow" "请输入要创建的用户名:")" name
|
||
if id "$name" &>/dev/null; then
|
||
color_echo "red" "用户 $name 已存在,请勿重复创建。"
|
||
else
|
||
useradd "$name"
|
||
userpasswd=$(openssl rand -base64 8)
|
||
echo "$userpasswd" | passwd --stdin "$name" &>/dev/null
|
||
if [ $? -eq 0 ]; then
|
||
echo "用户名:$name,密码:$userpasswd" >> /opt/user.txt
|
||
color_echo "green" "用户 $name 创建成功,密码已存储在 /opt/user.txt 中。"
|
||
else
|
||
color_echo "red" "用户创建失败,请检查系统配置。"
|
||
fi
|
||
fi
|
||
}
|
||
|
||
delete_user() {
|
||
read -p "$(color_echo "yellow" "请输入要删除的用户名:")" name
|
||
|
||
# 检查用户是否存在
|
||
if id "$name" &>/dev/null; then
|
||
# 删除用户及其主目录和邮件池
|
||
userdel -r "$name"
|
||
if [ $? -eq 0 ]; then
|
||
# 如果用户删除成功,尝试从记录文件中移除该用户的密码信息
|
||
sed -i "/^用户名:$name,/d" /opt/user.txt
|
||
color_echo "green" "用户 $name 已成功删除。"
|
||
else
|
||
color_echo "red" "用户删除失败,请检查系统配置。"
|
||
fi
|
||
else
|
||
color_echo "red" "用户 $name 不存在,无需删除。"
|
||
fi
|
||
}
|
||
|
||
# 安装常用软件
|
||
anzhuang() {
|
||
color_echo "yellow" "正在安装常用软件(vim、lsof、unzip、wget)..."
|
||
if yum -y install vim lsof unzip wget &>/dev/null; then
|
||
color_echo "green" "常用软件安装完成!"
|
||
else
|
||
color_echo "red" "软件安装失败,请检查 YUM 源配置。"
|
||
fi
|
||
}
|
||
|
||
mem() {
|
||
mem_total=$(free -m | awk 'NR==2 {print $2}')
|
||
mem_used=$(free -m | awk 'NR==2 {print $3}')
|
||
mem_free=$(free -m | awk 'NR==2 {print $4}')
|
||
mem_cache=$(free -m | awk 'NR==2 {print $6}')
|
||
mem_s=$((mem_used * 100 / mem_total))
|
||
mem_f=$((100 - mem_s))
|
||
color_echo "green" "内存的使用率:$mem_s%"
|
||
color_echo "green" "内存的总量: $mem_total MB"
|
||
color_echo "green" "内存的空闲率: $mem_f%"
|
||
color_echo "green" "内存的缓存量: $mem_cache MB"
|
||
color_echo "green" "内存的空闲量: $mem_free MB"
|
||
}
|
||
|
||
disk() {
|
||
read -p "$(color_echo "yellow" "请输入分区名称:")" name
|
||
case $name in
|
||
"/")
|
||
df -Th | awk 'NR==2 {print "总量:"$3,"使用:"$4,"空闲:"$5}' | while read line; do color_echo "green" "$line"; done
|
||
;;
|
||
"/boot")
|
||
df -Th | awk 'NR==7 {print "总量:"$3,"使用:"$4,"空闲:"$5}' | while read line; do color_echo "green" "$line"; done
|
||
;;
|
||
*)
|
||
color_echo "red" "无效的分区名称"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
system_info() {
|
||
cat /etc/redhat-release | awk '{print "系统版本号:"$4}' | while read line; do color_echo "green" "$line"; done
|
||
color_echo "green" "内核版本为:`uname -r`"
|
||
ip a | grep "2:" | awk -F: '{print "网卡名称:"$2}' | while read line; do color_echo "green" "$line"; done
|
||
ip a | grep "inet " | awk '{print $2}' | awk -F "/" '{print "IP地址是:"$1}' | while read line; do color_echo "green" "$line"; done
|
||
}
|
||
|
||
cpu_info() {
|
||
w | awk -F: 'NR==1 {print "CPU的平均负载:"$NF}' | while read line; do color_echo "green" "$line"; done
|
||
vmstat | awk 'NR==3 {print "CPU空闲率:"$(NF-2)}' | while read line; do color_echo "green" "$line"; done
|
||
cpu_id=$(vmstat | awk 'NR==3 {print $(NF-2)}')
|
||
cpu_free=$((100 - cpu_id))
|
||
color_echo "green" "CPU使用率: $cpu_free%"
|
||
}
|
||
|
||
# 主循环
|
||
while true; do
|
||
list
|
||
read -p "$(color_echo "yellow" "请输入您的选项(1-11):")" num
|
||
case $num in
|
||
1) yuan; sleep 2 ;;
|
||
2) fire; sleep 2 ;;
|
||
3) timed; sleep 2 ;;
|
||
4) user; sleep 2 ;;
|
||
5) delete_user; sleep 2;;
|
||
6) anzhuang; sleep 2 ;;
|
||
7) mem; sleep 2;;
|
||
8) disk; sleep 2;;
|
||
9) system_info; sleep 2;;
|
||
10) cpu_info; sleep 2;;
|
||
11) color_echo "cyan" "退出工具箱,感谢使用!"; exit ;;
|
||
*) color_echo "red" "输入无效,请输入数字 1-11 选择功能。"; sleep 2 ;;
|
||
esac
|
||
done |