更新 MD/centos.sh

This commit is contained in:
zwb 2025-03-17 16:19:57 +08:00
parent 1a17b1b47e
commit 8a9bd23a33

View File

@ -1,78 +1,109 @@
#!/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() {
echo "--------------------------------------"
echo "------------性能优化工具箱------------"
echo "--------------------------------------"
echo "1. 更换 YUM 源"
echo "2. 关闭防火墙"
echo "3. 时间同步"
echo "4. 创建用户"
echo "5. 删除用户"
echo "6. 安装常用软件"
echo "7. 内存使用情况"
echo "8. 磁盘使用情况"
echo "9. 服务器信息"
echo "10 CPU使用情况"
echo "11. 退出"
echo "--------------------------------------"
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() {
echo "正在更换 YUM 源..."
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
echo "YUM 源更换完成!"
color_echo "green" "YUM 源更换完成!"
else
echo "YUM 源更换失败,请检查网络或链接。"
color_echo "red" "YUM 源更换失败,请检查网络或链接。"
fi
}
# 关闭防火墙
fire() {
echo "正在关闭防火墙..."
color_echo "yellow" "正在关闭防火墙..."
systemctl stop firewalld
systemctl disable firewalld
setenforce 0 # 临时关闭 SELinux
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 永久关闭 SELinux
echo "防火墙已关闭SELinux 已禁用(需重启生效)。"
color_echo "green" "防火墙已关闭SELinux 已禁用(需重启生效)。"
}
# 时间同步
timed() {
echo "正在安装和同步时间..."
color_echo "yellow" "正在安装和同步时间..."
if yum -y install ntpdate &>/dev/null; then
ntpdate ntp1.aliyun.com
echo "时间同步成功!"
color_echo "green" "时间同步成功!"
else
echo "时间同步失败,请检查网络连接或 YUM 配置。"
color_echo "red" "时间同步失败,请检查网络连接或 YUM 配置。"
fi
}
# 创建用户
user() {
read -p "请输入要创建的用户名:" name
read -p "$(color_echo "yellow" "请输入要创建的用户名:")" name
if id "$name" &>/dev/null; then
echo "用户 $name 已存在,请勿重复创建。"
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
echo "用户 $name 创建成功,密码已存储在 /opt/user.txt 中。"
color_echo "green" "用户 $name 创建成功,密码已存储在 /opt/user.txt 中。"
else
echo "用户创建失败,请检查系统配置。"
color_echo "red" "用户创建失败,请检查系统配置。"
fi
fi
}
delete_user() {
read -p "请输入要删除的用户名:" name
read -p "$(color_echo "yellow" "请输入要删除的用户名:")" name
# 检查用户是否存在
if id "$name" &>/dev/null; then
@ -81,24 +112,25 @@ delete_user() {
if [ $? -eq 0 ]; then
# 如果用户删除成功,尝试从记录文件中移除该用户的密码信息
sed -i "/^用户名:$name/d" /opt/user.txt
echo "用户 $name 已成功删除。"
color_echo "green" "用户 $name 已成功删除。"
else
echo "用户删除失败,请检查系统配置。"
color_echo "red" "用户删除失败,请检查系统配置。"
fi
else
echo "用户 $name 不存在,无需删除。"
color_echo "red" "用户 $name 不存在,无需删除。"
fi
}
# 安装常用软件
anzhuang() {
echo "正在安装常用软件vim、lsof、unzip、wget..."
color_echo "yellow" "正在安装常用软件vim、lsof、unzip、wget..."
if yum -y install vim lsof unzip wget &>/dev/null; then
echo "常用软件安装完成!"
color_echo "green" "常用软件安装完成!"
else
echo "软件安装失败,请检查 YUM 源配置。"
color_echo "red" "软件安装失败,请检查 YUM 源配置。"
fi
}
mem() {
mem_total=$(free -m | awk 'NR==2 {print $2}')
mem_used=$(free -m | awk 'NR==2 {print $3}')
@ -106,56 +138,59 @@ mem() {
mem_cache=$(free -m | awk 'NR==2 {print $6}')
mem_s=$((mem_used * 100 / mem_total))
mem_f=$((100 - mem_s))
echo "内存的使用率:$mem_s%"
echo "内存的总量: $mem_total MB"
echo "内存的空闲率: $mem_f%"
echo "内存的缓存量: $mem_cache MB"
echo "内存的空闲量: $mem_free MB"
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 "请输入分区名称:" name
read -p "$(color_echo "yellow" "请输入分区名称:")" name
case $name in
"/")
df -Th | awk 'NR==2 {print "总量:"$3,"使用:"$4,"空闲:"$5}'
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}'
df -Th | awk 'NR==7 {print "总量:"$3,"使用:"$4,"空闲:"$5}' | while read line; do color_echo "green" "$line"; done
;;
*)
echo "无效的分区名称"
color_echo "red" "无效的分区名称"
;;
esac
}
system_info() {
cat /etc/redhat-release | awk '{print "系统版本号:"$4}'
echo "内核版本为:`uname -r`"
ip a | grep "2:" | awk -F: '{print "网卡名称:"$2}'
ip a | grep "inet " | awk '{print $2}' | awk -F "/" '{print "IP地址是:"$1}'
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}'
vmstat | awk 'NR==3 {print "CPU空闲率:"$(NF-2)}'
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))
echo "CPU使用率: $cpu_free%"
color_echo "green" "CPU使用率: $cpu_free%"
}
# 主循环
while true; do
list
read -p "请输入您的选项1-10" num
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;;
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;;
q) echo "退出工具箱,感谢使用!"; exit ;;
*) echo "输入无效,请输入数字 1-6 选择功能。"; sleep 2 ;;
10) cpu_info; sleep 2;;
11) color_echo "cyan" "退出工具箱,感谢使用!"; exit ;;
*) color_echo "red" "输入无效,请输入数字 1-11 选择功能。"; sleep 2 ;;
esac
done