shell脚本查看Linux硬件配置[cpu/内存/硬盘/网络]

脚本说明:

在RedHat5.4,5.5版本测试通过,显示CPU,内存,硬盘,网络的基本信息;运行后的结果是(虚拟机):

******************
******Detail******
Hostname: oracle-t-g
Machine_name:  VMware, Inc.
Version: Linux version 2.6.18-194.el5
Linux_Version: Red Hat Enterprise Linux Server release 5.5 (Tikanga)
Machine_kind:  VMware Virtual Platform
SN: VMware-42 34 61 30 46 cd c2 df-4b 61 49 96 ed 8a c3 4e
MT:     Product Name: VMware Virtual Platform
****CPU****
CPU_Name:  Intel(R) Xeon(R) CPU           X5650  @ 2.67GHz
Total_physical_CPU: 0
Total_core_num: 0
Total_Processor_num: 4
CPU_BIT: 64
*****Memory******
Total_Mem: 8175264  KB
Total_Mem: 7 G
Mem_Hz:
Mem_Max: 256 GB
Memory_Num: 30
Memory_Num_Used: 1
Mem_list:
        Size: 8192 MB
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Size: No Module Installed
        Range Size: 8 GB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
        Range Size: 1 kB
****Disk****
Total_Disk: Disk /dev/sda: 42.9 GB
****Net Info****
eth0_Mac_addr: 00:50:56:B4:00:4C
eth0_IP_addr: 172.16.4.30
net_list:
eth0
lo
******fb*******

 内容如下:

#!/bin/bash
#************************************
# Program:detect CPU,memory,disk,net
# History:1/10/2013 zhaofei 1.0
# Note: Linux 5.4,5.5
# History:None
# To_File_Command:sh sh01.sh >1.txt
#************************************

显示基本信息:

主要是使用dmidecode命令查看基本的信息,取System Inforamtion行下面的行,使用sed -n '/System Information/{n;n;p;}'

#Machine_Detail
hostname=$(hostname)
machine_name=$(dmidecode|sed -n '/System Information/{n;p;}'|sed 's/^.*://g')
version=$(cat /proc/version|sed 's/(.*$//g')
linux_version=$(head -n 1 /etc/issue)
machine_number=$(dmidecode|sed -n '/System Information/{n;n;p;}'|sed 's/^.*\[//g'|sed 's/\].*$//g')
machine_kind=$(dmidecode|sed -n '/System Information/{n;n;p;}'|sed 's/.*://g'|sed 's/-.*$//g')
machine_SN=$(dmidecode|sed -n '/System Information/{n;n;n;n;p;}'|sed 's/^.*: //g')
echo "******************"
echo "******Detail******"
echo "Hostname: $hostname"
echo "Machine_name: $machine_name"
echo "Version: $version"
echo "Linux_Version: $linux_version"
echo "Machine_kind: $machine_kind"
echo "SN: $machine_SN"
echo "MT: $machine_number"

CPU:

主要是查看/proc/cpuinfo里面的内容,对physical id和core id做统计,得出的物理/逻辑CPU和进程数

#CPU
total_physical_CPU_num=$(cat /proc/cpuinfo|grep "physical id"|sort|uniq|wc -l)
cpu_bit=$(getconf LONG_BIT)
core_num_per_physical=$(cat /proc/cpuinfo|grep "core id"|sort|uniq|wc -l)
total_core_num=$(($core_num_per_physical*$total_physical_CPU_num))
total_processor_num=$(cat /proc/cpuinfo|grep "processor"|wc -l)
CPU_name=$(cat /proc/cpuinfo|grep "model name"|uniq|sed 's/^.*://g')
echo "****CPU****"
echo "CPU_Name: $CPU_name"
echo "Total_physical_CPU: $total_physical_CPU_num"
echo "Total_core_num: $total_core_num"
echo "Total_Processor_num: $total_processor_num"
echo "CPU_BIT: $cpu_bit"

内存:

还是使用dmidecode,提取Memory Device行下的16行内容,使用grep -A16 "Memory Device"

#Memory
total_mem=$(grep MemTotal /proc/meminfo | sed 's/^[^0-9]*//g'|sed 's/kB//g')
total_mem_g=$(($total_mem/1048576))
memory_Hz=$(dmidecode|grep -A16 "Memory Device" | grep 'Speed'|uniq |grep 'Hz'|sed 's/^.*: //g'|uniq)
memory_max=$(dmidecode | grep  "Maximum Capacity"|sed 's/^.*: //g')
memory_list=$(dmidecode|grep  -A5 "Memory Device"|grep Size)
memory_num=$(dmidecode|grep -A5 "Memory Device"|grep Size -c)
memory_num_used=$(dmidecode|grep -A5 "Memory Device"|grep MB -c)
echo "*****Memory******"
echo "Total_Mem: $total_mem KB"
echo "Total_Mem: $total_mem_g G"
echo "Mem_Hz: $memory_Hz"
echo "Mem_Max: $memory_max"
echo "Memory_Num: $memory_num"
echo "Memory_Num_Used: $memory_num_used"
echo "Mem_list:"
echo "$memory_list"

Disk&Net

一般的服务器会有eth0和eth1,判断是否有eth0和eth1,并根据情况进行输出,并输出net_list检查有没有其他的网卡

#Disk
total_disk=$(fdisk -l | grep '^Disk'| sed 's/,.*//g')
echo "****Disk****"
echo "Total_Disk: $total_disk"

#Net
echo "****Net Info****"
net_list=$(ifconfig |grep 'Link encap:'|sed 's/ .*$//g')
is_eth0=$(ifconfig|grep 'eth0' -c)
is_eth1=$(ifconfig|grep 'eth1' -c)
[ is_eth0 != 0 ] && ip_addr=$(ifconfig eth0|grep 'inet addr' | sed 's/^.*inet addr://g'|sed 's/ .*$//g')
[ is_eth0 != 0 ] && mac_addr=$(ifconfig eth0|grep "HWaddr"| sed 's/^.*HWaddr //g')
[ $is_eth1 != 0 ] &&  eth1_mac_addr=$(ifconfig eth1|grep "HWaddr"| sed 's/^.*HWaddr //g')
[ $is_eth1 != 0 ] && eth1_ip_addr=$(ifconfig eth1|grep 'inet addr'|sed 's/^.*inet addr://g'|sed 's/ .*$//g')
[ "$mac_addr"x != ""x ] && echo "eth0_Mac_addr: $mac_addr"
[ "$ip_addr"x != ""x ] && echo "eth0_IP_addr: $ip_addr"
[ "$eth1_mac_addr"x != ""x ] && echo "eth1_Mac_addr: $eth1_mac_addr"
[ "$eth1_ip_addr"x != ""x ] && echo "eth1_IP_addr: $eth1_ip_addr"
echo "net_list:"
echo "$net_list"

FB卡:

在/sys/class/fc_host/下的port_name下

echo "******fb*******"
fb=$(lspci|grep Fibre|sed 's/^.*: //g')
[ -d "/sys/class/fc_host/" ] && fb_list=$(ls /sys/class/fc_host/)
[ "$fb"x != ""x ]&& echo "FB_Card: $fb"
for i in $fb_list
do
 fb_port_num=$(cat /sys/class/fc_host/$i/port_name)
 echo "fb_port_number: $fb_port_num" 
done

 最后是脚本全文:

View Code
#!/bin/bash
#************************************
# Program:detect CPU,memory,disk,net
# History:1/10/2013 zhaofei 1.0
# Note: Linux 5.4,5.5
# History:None
# To_File_Command:sh sh01.sh >1.txt
#************************************

#Machine_Detail
hostname=$(hostname)
machine_name=$(dmidecode|sed -n '/System Information/{n;p;}'|sed 's/^.*://g')
version=$(cat /proc/version|sed 's/(.*$//g')
linux_version=$(head -n 1 /etc/issue)
machine_number=$(dmidecode|sed -n '/System Information/{n;n;p;}'|sed 's/^.*\[//g'|sed 's/\].*$//g')
machine_kind=$(dmidecode|sed -n '/System Information/{n;n;p;}'|sed 's/.*://g'|sed 's/-.*$//g')
machine_SN=$(dmidecode|sed -n '/System Information/{n;n;n;n;p;}'|sed 's/^.*: //g')
echo "******************"
echo "******Detail******"
echo "Hostname: $hostname"
echo "Machine_name: $machine_name"
echo "Version: $version"
echo "Linux_Version: $linux_version"
echo "Machine_kind: $machine_kind"
echo "SN: $machine_SN"
echo "MT: $machine_number"

#CPU
total_physical_CPU_num=$(cat /proc/cpuinfo|grep "physical id"|sort|uniq|wc -l)
cpu_bit=$(getconf LONG_BIT)
core_num_per_physical=$(cat /proc/cpuinfo|grep "core id"|sort|uniq|wc -l)
total_core_num=$(($core_num_per_physical*$total_physical_CPU_num))
total_processor_num=$(cat /proc/cpuinfo|grep "processor"|wc -l)
CPU_name=$(cat /proc/cpuinfo|grep "model name"|uniq|sed 's/^.*://g')
echo "****CPU****"
echo "CPU_Name: $CPU_name"
echo "Total_physical_CPU: $total_physical_CPU_num"
echo "Total_core_num: $total_core_num"
echo "Total_Processor_num: $total_processor_num"
echo "CPU_BIT: $cpu_bit"

#Memory
total_mem=$(grep MemTotal /proc/meminfo | sed 's/^[^0-9]*//g'|sed 's/kB//g')
total_mem_g=$(($total_mem/1048576))
memory_Hz=$(dmidecode|grep -A16 "Memory Device" | grep 'Speed'|uniq |grep 'Hz'|sed 's/^.*: //g'|uniq)
memory_max=$(dmidecode | grep  "Maximum Capacity"|sed 's/^.*: //g')
memory_list=$(dmidecode|grep  -A5 "Memory Device"|grep Size)
memory_num=$(dmidecode|grep -A5 "Memory Device"|grep Size -c)
memory_num_used=$(dmidecode|grep -A5 "Memory Device"|grep MB -c)
echo "*****Memory******"
echo "Total_Mem: $total_mem KB"
echo "Total_Mem: $total_mem_g G"
echo "Mem_Hz: $memory_Hz"
echo "Mem_Max: $memory_max"
echo "Memory_Num: $memory_num"
echo "Memory_Num_Used: $memory_num_used"
echo "Mem_list:"
echo "$memory_list"

#Disk
total_disk=$(fdisk -l | grep '^Disk'| sed 's/,.*//g')
echo "****Disk****"
echo "Total_Disk: $total_disk"

#Net
echo "****Net Info****"
net_list=$(ifconfig |grep 'Link encap:'|sed 's/ .*$//g')
is_eth0=$(ifconfig|grep 'eth0' -c)
is_eth1=$(ifconfig|grep 'eth1' -c)
[ is_eth0 != 0 ] && ip_addr=$(ifconfig eth0|grep 'inet addr' | sed 's/^.*inet addr://g'|sed 's/ .*$//g')
[ is_eth0 != 0 ] && mac_addr=$(ifconfig eth0|grep "HWaddr"| sed 's/^.*HWaddr //g')
[ $is_eth1 != 0 ] &&  eth1_mac_addr=$(ifconfig eth1|grep "HWaddr"| sed 's/^.*HWaddr //g')
[ $is_eth1 != 0 ] && eth1_ip_addr=$(ifconfig eth1|grep 'inet addr'|sed 's/^.*inet addr://g'|sed 's/ .*$//g')
[ "$mac_addr"x != ""x ] && echo "eth0_Mac_addr: $mac_addr"
[ "$ip_addr"x != ""x ] && echo "eth0_IP_addr: $ip_addr"
[ "$eth1_mac_addr"x != ""x ] && echo "eth1_Mac_addr: $eth1_mac_addr"
[ "$eth1_ip_addr"x != ""x ] && echo "eth1_IP_addr: $eth1_ip_addr"
echo "net_list:"
echo "$net_list"
echo "******fb*******"
fb=$(lspci|grep Fibre|sed 's/^.*: //g')
[ -d "/sys/class/fc_host/" ] && fb_list=$(ls /sys/class/fc_host/)
[ "$fb"x != ""x ]&& echo "FB_Card: $fb"
for i in $fb_list
do
 fb_port_num=$(cat /sys/class/fc_host/$i/port_name)
 echo "fb_port_number: $fb_port_num" 
done
原文地址:https://www.cnblogs.com/zhaofei2013/p/2859790.html