檢查系統信息的簡單腳本

#!/bin/bash
#檢查系統信息
#2016-09-21
#Version 2.0.1

#檢查當前用戶是否爲root
if [ `id -u` -ne 0  ]
then
        echo '請使用root用戶執行腳本!';
        exit 11;
fi

release_file='/etc/redhat-release';
smart_bin=`which smartctl 2>/dev/null`;
dmi_bin=`which dmidecode  2>/dev/null`;
#smart=null;
diskinfo=(`fdisk -l|awk 'FS="[ :,.]+"{if($0~/Disk \/.+\/[a-z]+:/){print $2":"$3$5}}'`);

#檢查是否安裝"smartmontools","smartmontools"包含smartctl
if [ -n $smart_bin  ]
then
        smart=true;
fi

#檢查是否安裝"dmidecode"
if [ -n $dmi_bin  ]
then
        dmi=true;
fi

if [ -e $release_file ]
then
        echo '操做系統名稱:'`cat $release_file`;
fi

if [ $dmi == 'true'  ]
then
        echo -ne '主板信息:\n';
        dmidecode -t 2|awk 'FS=":"{if($0~/Manufacturer/ && $2 !~/Not Specified/){manufacturer=$2;}if($0~/Product Name/ && $2 !~/Not Specified/){product=$2;}}END{print  manufacturer" --"product;}';
        echo -ne '內存信息:\n';
        dmidecode -t 17|awk 'FS=":"{if($0 ~"Size:" && $2 !~ / No Module Installed/){num=NR+9}if(NR>num){b=0}else{b=1}}b{if($0 ~/Size:/ || $0 ~/[^ ]Speed:/ || $0 ~/Manufacturer:/){gsub(/\s/,"",$0);print $0}}';

fi

echo -ne '處理器信息:\n';
        awk 'FS=": "{if($1~"model name"){print $2}}' /proc/cpuinfo;

        
echo -ne '磁盤驅動:\n';
        #輸出數組中的內容
        for dev in ${diskinfo[*]}
        do
                dev_num=`echo $dev|cut -d':' -f1`;
                dev_type=`smartctl -i $dev_num|awk 'FS=":"{if($1~"Device Model"){print $2}}'`;

                if [ $smart == 'true'  ]
                then
                        echo -ne $dev_type"\n";
                        echo -ne $dev"\n";
                else
                        echo -ne $dev"\n";
                fi

        done

echo -ne '網絡適配器:\n';
        lspci|awk 'FS=":"{if($0~"Ethernet controller"){print $3}}'

echo

if [ $smart == 'null' -o $dmi == 'null'  ]
then
        echo '如需獲取詳細信息請使用"smartmontools"和"dmidecode"工具'
fi

運行結果:數組

wKiom1fiWG6goFozAABel4LwRzA200.png-wh_50

相關文章
相關標籤/搜索