歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

linux 腳本實現一鍵查看硬件配置

自己又接手了兩個項目,突然老大問我服務器什麼配置,當時我就懵了,一直沒太在意這個事情,只是保證服務器正常運行,網站能夠正常訪問,其它的沒有做,於是今天寫了個腳本,把自己想知道的配置都在這個腳本中寫了出來。在紅帽和centos系統上測試沒有問題。腳本如下,希望可以共同學習。

#!/bin/bash

dir=`pwd`

proName(){
    productName=`dmidecode | grep Product|sed 's/^[ \t]*//g'`
    echo -e "\033[32;49;1m [服務器型號] \033[39;49;0m" > $dir/confTab.txt
    echo -e "$productName" >> $dir/confTab.txt
}

sysVersion(){
    sysVersion=`cat /etc/issue|sed -n "1p"`
    echo -e "\033[32;49;1m [系統版本] \033[39;49;0m" >> $dir/confTab.txt
    echo -e "$sysVersion" >> $dir/confTab.txt
    
}

coreVersion(){
    coreVersion=`uname -r`
    echo -e "\033[32;49;1m [內核版本] \033[39;49;0m" >> $dir/confTab.txt
    echo -e "$coreVersion" >> $dir/confTab.txt
}
cpuInfo(){
    logicalNum=`cat /proc/cpuinfo |grep "processor"|wc -l`
    pysicalNum=`cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l`
    pysicalCoreNum=`cat /proc/cpuinfo| grep "cpu cores"| uniq |awk '{print $4}'`
    otherInfo=`cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c |awk '{print $2,$3,$7}'`
    firstCache=`cat /sys/devices/system/cpu/cpu0/cache/index0/size`
    let coreTotal=pysicalNum*pysicalCoreNum
    echo -e "\033[32;49;1m [cpu信息] \033[39;49;0m" >> $dir/confTab.txt
    echo -e "`getconf LONG_BIT`位,物理$coreTotal核($pysicalNum X $pysicalCoreNum),邏輯$logicalNum核,$otherInfo,一級緩存$firstCache" >> $dir/confTab.txt
}

memInfo(){
    memSize=`free -m | grep Mem | awk '{print $2}'`
    swapSize=`free -m | grep Swap | awk '{print $2}'`
    echo -e "\033[32;49;1m [內存信息] \033[39;49;0m" >> $dir/confTab.txt
    echo -e "內存:$memSize Mb,交換分區:$swapSize Mb" >> $dir/confTab.txt
}

diskInfo(){
    diskInfo=`fdisk -l|grep "Disk /dev/sd"|tr "," ":" |awk -F ":" '{print $1,$2}'`
    echo -e "\033[32;49;1m [硬盤信息] \033[39;49;0m" >> $dir/confTab.txt
    echo -e "$diskInfo" >> $dir/confTab.txt
}

netInfo(){
    echo -e "\033[32;49;1m [網卡信息] \033[39;49;0m" >> $dir/confTab.txt
    for i in `ifconfig|egrep -E "em|eth"|grep -v Interrupt| awk '{print $1}'`
    do
    echo -e "\033[33;49;1m$i  \033[39;49;0m`ethtool $i | egrep -E "Speed|Duplex" | sed "s/^[ \t]//g"|sed -re '1,${N;s/\n/ /}' `" >> $dir/confTab.txt
    done
}

proName
sysVersion
coreVersion
cpuInfo
memInfo
diskInfo
netInfo
本文出自 “庭中有奇樹” 博客,請務必保留此出處http://zhangdl.blog.51cto.com/11050780/1833674

Copyright © Linux教程網 All Rights Reserved