教程

  1. 打开你Linux系统的SSH功能,使用终端软件连接并切换到root状态下。

  2. 进入/etc/profile.d/目录下

    cd /etc/profile.d/
  3. 编辑创建一个名称为systeminfo.sh的脚本文件

    vi systeminfo.sh
  4. 按字母 i 键进入编辑模式,复制下面的配置文件,粘贴到终端,按Esc键退出编辑模式,输入 :wq 退出并保存。

    #!/bin/bash
    # by 运维朱工(脚本作者)
    # site:bash.lutixia.cn
    ####################################
    
    
    
    # 获取IP地址和主机名
    IP_ADDR=$(hostname -I | cut -d' ' -f1)
    HOSTNAME=$(hostname)
    
    # CPU负载信息:
    cpu_load() {
        echo -e "\t\t\tcpu的负载情况"
        echo -e "\t------------------------------------------------"
        echo -e "\tCPU load in 1  min is: `awk  '{printf "%15s",$1}' /proc/loadavg`"
        echo -e "\tCPU load in 5  min is: `awk  '{printf "%15s",$2}' /proc/loadavg`"
        echo -e "\tCPU load in 10 min is: `awk  '{printf "%15s",$3}' /proc/loadavg`"
        echo
    }
    
    # mem基本信息:
    memory_info() {
        echo -e "\t\t\t内存的使用情况"
        echo -e "\t------------------------------------------------"
        echo -e "\t`free -h | awk '/Mem/{printf "%-10s %s","内存总容量:",$2}'`"
        echo -e "\t`free -h | awk '/Mem/{printf "%-10s %s","内存空闲容量:",$4}'`"
        echo -e "\t`free -h | awk '/Mem/{printf "%-10s %s","内存缓存:",$6}'`"
        echo
    }
    
    # 磁盘使用量排序:
    disk_rank() {
        echo -e "\t\t\t各分区使用率"
        echo -e "\t------------------------------------------------"
        df -h  -x tmpfs -x devtmpfs | sort -nr -k 5 | awk '/dev/{printf "\t%-20s %10s\n", $1, $5}'
        echo
    }
    
    # 显示系统信息
    echo
    echo -e "\t\t\t系统基本信息:"
    echo -e "\t------------------------------------------------"
    echo -e "\tCurrent Time : $(date)"
    echo -e "\tVersion      : $(cat /etc/os-release | grep -w "PRETTY_NAME" | cut -d= -f2 | tr -d '"')"
    echo -e "\tKernel       : $(uname -r)"
    echo -e "\tUptime       : $(uptime -p)"
    echo -e "\tIP addr      : $IP_ADDR"
    echo -e "\tHostname     : $HOSTNAME"
    echo -e "\tCpu          : $(lscpu | grep "Model name:" | sed 's/Model name:\s*//')"
    echo -e "\tMemory       : $(free -h | awk '/^Mem:/ { print $3 "/" $2 }')"
    echo -e "\tSWAP         : $(free -h | awk '/^Swap:/ { print $3 "/" $2 }')"
    echo -e "\tUsers Logged : $(who | wc -l) users"
    echo
    
    cpu_load
    memory_info
    disk_rank
    
  5. 授权脚本文件执行权限

    chmod +x /etc/profile.d/systeminfo.sh
  6. 然后你每次使用终端软件连接此设备的SSH时,都会显示系统信息。

  7. 演示直接在黑群晖虚拟机上直接执行脚本文件的效果(黑群晖上无/etc/profile.d/路径)

    root@nas:/volume2/docker# ./sysinfo.sh   #执行脚本文件(脚本文件随意命名)
    hostname: invalid option -- 'I'
    Usage: hostname [-v] {hostname|-F file}      set hostname (from file)
           domainname [-v] {nisdomain|-F file}   set NIS domainname (from file)
           hostname [-v] [-d|-f|-s|-a|-i|-y|-n]  display formatted name
           hostname [-v]                         display hostname
    
           hostname -V|--version|-h|--help       print info and exit
    
        dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y
    
        -s, --short           short host name
        -a, --alias           alias names
        -i, --ip-address      addresses for the hostname
        -f, --fqdn, --long    long host name (FQDN)
        -d, --domain          DNS domain name
        -y, --yp, --nis       NIS/YP domainname
        -F, --file            read hostname or NIS domainname from given file
    
       This command can read or set the hostname or the NIS domainname. You can
       also read the DNS domain or the FQDN (fully qualified domain name).
       Unless you are using bind or NIS for host lookups you can change the
       FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
       part of the FQDN) in the /etc/hosts file.
    
                            系统基本信息:
            ------------------------------------------------
            Current Time : Fri Jun 21 05:35:05 PM CST 2024
    cat: /etc/os-release: No such file or directory
            Version      :
            Kernel       : 4.4.302+
            Uptime       : up 3 days, 2 hours, 24 minutes
            IP addr      :
            Hostname     : nas
    ./sysinfo.sh: line 50: lscpu: command not found
            Cpu          :
            Memory       : 359Mi/3.8Gi
            SWAP         : 2.0Mi/8.3Gi
            Users Logged : 1 users
    
                            cpu的负载情况
            ------------------------------------------------
            CPU load in 1  min is:            0.13
            CPU load in 5  min is:            0.12
            CPU load in 10 min is:            0.09
    
                            内存的使用情况
            ------------------------------------------------
            内存总容量:     3.8Gi
            内存空闲容量:    1.3Gi
            内存缓存:      2.1Gi
    
                            各分区使用率
            ------------------------------------------------
            /dev/mapper/cachedev_0        60%
            /dev/md0                    17%
    
    

参考资料

  • 网络资料地址:https://mp.weixin.qq.com/s/6bNvXmcJtE05UmGbWsh76w

  • 脚本文件下载地址:https://www.alipan.com/s/UGFcYNmRUbE

功德+1(狗子).gif

👇👇👇