shell脚本——系统工具箱(SystemToolbox)

2023-11-19

一些想法

  1. 需要使用shell编写一个简单实用的系统工具箱脚本
  2. 一共想出了两套方案
  3. 本套综合了一些自己的想法不是很熟练大家看个乐 (^o ^ )

基本使用界面大概是这样
在这里插入图片描述

分析需要的功能

我的构思将其分成7个部分

  1. 磁盘挂载信息部分
  2. 内存使用部分
  3. CPU利用率和负载部分
  4. 网络端口信息部分
  5. 进程信息部分
  6. 磁盘每秒进程下的IO读、写请求数量部分
  7. 没有匹配项重新显示菜单部分

构建整体框架

1、定义了函数

#<-----------------------询问继续使用工具箱函数定义
whether () { read -p "Continue to use[y],sign out[n]:" play
        if [[ "$play" == "y" ]];then
        clear
        else
                echo "Thank you for your use. Have a nice day!";exit
        fi
}
#<-------------------------菜单显示函数定义
ToolboxMenu () {
echo -e "\033[5;34m ========This is a system detection toolbox============\033[0m"
echo -e "\033[33m |            1.查看磁盘挂载信息                           | \033[0m"
echo -e "\033[33m |            2.查看内存信息                              | \033[0m"
echo -e "\033[33m |            3.查看CPU信息                               | \033[0m"
echo -e "\033[33m |            4.查看网络端口信息                           | \033[0m"
echo -e "\033[33m |            5.查看进程信息                              | \033[0m"
echo -e "\033[33m |            6.磁盘每秒进程下的I0读写数量                  | \033[0m"
echo -e "\033[33m |            7.Exit Toolbox                            | \033[0m"
echo -e "\033[33m | Current Time:$(date "+%Y-%m-%d %H:%M:%S") welcome!!engineer! |\033[0m"
echo -e "\033[5;34m =============System detection toolbox=================\033[0m"
}

2、使用交互定义变量邀请用户输入

#<----------------邀请用户输入需要检测的信息
read -p "Please enter your choice![1..7]" number

3、使用while循环让工具箱一直使用
4、中间为case判断选项
5、最后一部分为没有匹配项的匹配

#<------------------没有匹配项重新显示菜单
                *)
                        echo -e "\033[31m  Error redirecting to menu for you ! \033[0m"
                ;;

着手完整代码

#!/bin/bash
#Author:Pakho
#Date:2021.5.21
#File:SystemToolbox.sh
#VersionNumber:v1.0.1
#https://blog.csdn.net/sixeleven611
#Demand:Write system toolbox
#——————————————————————————————————
#script start
#<-----------------------询问继续使用工具箱函数定义
whether () { read -p "Continue to use[y],sign out[n]:" play
        if [[ "$play" == "y" ]];then
        clear
        else
                echo "Thank you for your use. Have a nice day!";exit
        fi
}
#菜单显示函数定义
ToolboxMenu () {
echo -e "\033[5;34m ========This is a system detection toolbox============\033[0m"
echo -e "\033[33m |            1.查看磁盘挂载信息                          | \033[0m"
echo -e "\033[33m |            2.查看内存信息                             | \033[0m"
echo -e "\033[33m |            3.查看CPU信息                             | \033[0m"
echo -e "\033[33m |            4.查看网络端口信息                          | \033[0m"
echo -e "\033[33m |            5.查看进程信息                             | \033[0m"
echo -e "\033[33m |            6.磁盘每秒进程下的I0读写数量                 | \033[0m"
echo -e "\033[33m |            7.Exit Toolbox                           | \033[0m"
echo -e "\033[33m | Current Time:$(date "+%Y-%m-%d %H:%M:%S") welcome!!engineer! |\033[0m"
echo -e "\033[5;34m =============System detection toolbox=================\033[0m"
}
#<----------------邀请用户输入需要检测的信息
while :
do
        ToolboxMenu
        read -p "Please enter your choice![1..7]" number
                case $number in
#<-----------------------------------------------------------磁盘挂载信息
                1)
                                echo -e "\033[5;34m ============Partition information${z}============\033[0m"
                                df -hT
                                whether
                ;;
#<------------------------------------------------------内存使用情况
                2)
                        echo -e "\033[5;34m ============Memory usage============\033[0m"
                        free -h
                        n=1
                        for i in {1..3}
                        do
                                echo -e "\033[5;34m ============Memory usage${n}============\033[0m"
                                vmstat | awk '{if(NR==3){print "You have free memory left:" $4}}'
                                let ++n
                                sleep 1
                        done
                        whether
                ;;
#<-------------------------------------------------------CPU利用率和负载
                3)
                        v=1
                        for i in {1..3}
                        do
                                echo -e "\033[5;34m ============CPU usage${v}===========\033[0m"
                                uptime
                                vmstat | grep 2 |awk '{if (100-$15<=10){print 100-$15 "%","您的CPU很安全"}else{print 100-$15 "%","您的CPU即将满载请留意"}}'
                                let ++v
                                sleep 1
                        done
                                whether
                ;;
#<---------------------------------------------------------------网络端口信息
                4)
                        while :
                        do
                                echo -e "\033[5;34m ============Network port information============\033[0m"
                                read -p "Please enter the network port number you want to query:" Port
                                netstat -anpt | head -2
                                netstat -anpt | grep $Port
                                read -p "Do you want to check the port?[y/n]" see
                                if [[ ! $see == "y" ]];then
                                        echo "Thank you for your use. Have a nice day!";exit
                                else
                                        break
                                fi
                        done
                        whether
                ;;
#<----------------------------------------------------------------进程信息查询
                5)
                        echo -e "\033[5;34m ============Process information============\033[0m"
                        read -p "Please enter the process information you want to query:" Process
                        ps aux | grep -v grep |grep $Process
                        whether
                ;;
#<-------------------------------------------磁盘每秒进程下的IO读、写请求数量
                6)
                        b=1
                        for i in {1..3}
                        do
                                echo -e "\033[5;34m ============Number of IO read / write requests issued by the process${b}============\033[0m"
                                iostat | grep ^sd | awk '{print $1,$2}'
                                let ++b
                                sleep 1
                        done
                                whether
                ;;
#<---------------------------------退出
                7)
                        echo -e "\033[36 Thank you for your use. Have a nice day!\033[0m"
                        exit
                ;;
#<------------------没有匹配项重新显示菜单
                *)
                        echo -e "\033[31m  Error redirecting to menu for you ! \033[0m"
                ;;
        esac
done
#script end                       
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

shell脚本——系统工具箱(SystemToolbox) 的相关文章

随机推荐