Linux系统管理程序

 

#!/bin/bash

TITLE="Linux系统管理程序"
#LOGFILE="/var/log/system_status.log"

# 查询CPU使用率
function cpu_usage() {  
    #echo "---- CPU 使用情况 ----" >> $LOGFILE  
    #top -bn1 | grep "Cpu(s)" | awk '{print "CPU 使用率: " $2 "%"}' >> $LOGFILE
    cpud=$(top -bn1 | grep "Cpu(s)" | awk '{print "使用率: " $2 "%"}')
    whiptail --title "$TITLE" --msgbox "CPU:\n$cpud" 20 50
}

# 查询内存使用情况
function memory_usage() {  
    #echo "---- 内存使用情况 ----" >> $LOGFILE
    #free -h | awk 'NR==2{printf "总内存: " $2 ", 已用: " $3 ", 剩余: " $4}' >> $LOGFILE
    #free | awk 'NR==2{printf "内存使用率:%.2f%%",($3 / $2 * 100)}' >> $LOGFILE
    percent=$(free | awk 'NR==2{printf "%.2f%%",($3 / $2 * 100)}')
    freed=$(free -h | awk 'NR==2{printf "总内存: " $2 ", 已用: " $3 ", 剩余: " $4}')
    whiptail --title "$TITLE" --msgbox "内存使用情况:\n$freed\n内存使用率:$percent" 20 50
}

# 查询磁盘使用情况
function disk_usage() {  
    #echo "---- 磁盘使用情况 ----" >> $LOGFILE  
    #df -h | awk '$NF=="/"{print "文件系统: " $1 ", 已用: " $3 ", 可用: " $4 ", 使用率: " $5}' >> $LOGFILE  
    diskd=$(df -h | awk '$NF=="/"{print "文件系统: " $1 ", 已用: " $3 ", 可用: " $4 ", 使用率: " $5}')
    whiptail --title "$TITLE" --msgbox "磁盘:\n$diskd" 20 50
}

# 统计tcp连接数 
function tcp_status() {  
    #echo "---- TCP 连接状态 ----" >> $LOGFILE  
    #ss -antp | awk '{status[$1]++}END{for(i in status) printf i":"status[i]" "}' >> $LOGFILE
    tcpd=$(ss -antp | awk '{status[$1]++}END{for(i in status) printf i":"status[i]" "}')
    whiptail --title "$TITLE" --msgbox "TCP:\nTCP连接统计:$tcpd" 20 50
}

# 查询接口带宽使用
function network_usage() {  
    #echo "---- 网络带宽使用情况 ----" >> $LOGFILE
    #ifstat -t 1 1 | awk 'NR==6{print "接收: " $2 " KB/s, 发送: " $4 " KB/s"}' >> $LOGFILE
    networkd=$(ifstat -t 1 | awk '/eth|ens/{print "接口:" $1 ", " "接收: " $2 " KB/s, 发送: " $4 " KB/s"}')
    whiptail --title "$TITLE" --msgbox "接口带宽:\n$networkd" 20 50
}

# 列出资源占用最高的进程
function top_process() {
    #echo "---- 占用资源最多的进程 ----" >> $LOGFILE
    #ps aux --sort=-%cpu | head -n 5 | awk '{printf "进程ID: %s, CPU: %.2f%%, 内存: %.2f%%, 命令: %s\n", $2, $3, $4, $11}' >> $LOGFILE
    procd=$(ps aux --sort=-%cpu | head -n 5 | awk 'NR>1{printf "用户: %s, 进程ID: %s, CPU: %.2f%%, 内存: %.2f%%, 命令: %s\n", $1, $2, $3, $4, $11}')
    whiptail --title "$TITLE" --msgbox "占用资源最多进程:\n$procd" 20 50
}

# 使用whiptail创建菜单
while true;do
    OPTION=$(whiptail --title "$TITLE" --menu "请选择相应的系统管理项:" 20 50 10 \
             "1" "查询CPU使用率" \
             "2" "查询内存使用肯况" \
             "3" "查询磁盘使用情况" \
             "4" "统计TCP连接数" \
             "5" "查询网络接口带宽" \
             "6" "列出最占用资源的进程" \
             "7" "退出程序" 3>&1 1>&2 2>&3)

    # 通过检查exitstatus的值来确定用户是否取消操作(如果用户点取消,exitstatus将不等于0)
    exitstatus=$? 

    # 检查用户的选择  
    case $OPTION in
        1)
            cpu_usage
            ;;
        2)
            memory_usage
            ;;
        3)
            disk_usage
            ;;
        4)
            tcp_status
            ;;
        5)
            network_usage
            ;;
        6)
            top_process
            ;;
        7)
            break
            ;;
    esac
    #whiptail --title "$TITLE" --msgbox "返回值:$exitstatus" 20 50
    [ $exitstatus != 0 ] && exit 0
done

whiptail处理多个菜单

#!/bin/bash

while true; do  
    main_choice=$(whiptail --title "主菜单" --menu "请选择一个选项" 15 60 3 \
    "1" "选项 1" \
    "2" "选项 2" \
    "3" "退出" 3>&1 1>&2 2>&3)  

    # 通过检查exitstatus的值来确定用户是否取消操作(如果用户按ESC键或者点取消,exitstatus将不等于0)
    exitstatus=$?  
    if [ $exitstatus = 0 ]; then  
        case $main_choice in
            1)  
                while true; do
                    sub_choice=$(whiptail --title "选项 1" --menu "选择子选项" 15 60 3 \
                    "1" "子选项 1" \
                    "2" "返回主菜单" \
                    "3" "退出" 3>&1 1>&2 2>&3)  

                    if [ $? = 0 ]; then  
                        if [ "$sub_choice" == "2" ]; then  
                            break  # 返回主菜单  
                        elif [ "$sub_choice" == "3" ]; then  
                            exit 0  # 退出  
                        else  
                            whiptail --msgbox "您选择了子选项 1" 8 45  
                        fi  
                    else  
                        whiptail --msgbox "操作已取消" 8 45  
                    fi  
                done  
                ;;  
            2)  
                whiptail --msgbox "您选择了选项 2" 8 45  
                ;;  
            3)  
                break  
                ;;  
        esac  
    else  
        whiptail --msgbox "操作已取消" 8 45  
    fi  
done

 dialog命令

#!/bin/bash  

# https://www.cnblogs.com/klb561/p/9043142.html  # linux dialog详解(图形化shell)
# https://www.zentao.net/blog/doing-windows-81897.html  # linux dialog

dialog
[ $? !=0 ] && yum -y install dialog

# 显示欢迎消息  
dialog --msgbox "Welcome to the dialog demo!" 6 40  

# 获取用户输入  
dialog --inputbox "What is your name?" 8 40 2> name.txt  
NAME=$(<name.txt)  

# 显示选择菜单  
dialog --menu "Hello, $NAME! Choose an option:" 15 50 4 \
1 "Option 1" \
2 "Option 2" \
3 "Exit" 2> option.txt  

OPTION=$(<option.txt)  

# 根据选择做出反应  
case $OPTION in  
    1) dialog --msgbox "You selected Option 1." 5 30 ;;  
    2) dialog --msgbox "You selected Option 2." 5 30 ;;  
    3) dialog --msgbox "Goodbye, $NAME!" 5 30 ;;  
esac

 

posted @ 2025-02-11 11:22  風£飛  阅读(30)  评论(0)    收藏  举报