获取本机硬件码

5210阅读 0评论2013-02-23 静飞lv
分类:LINUX

将本机硬件信息(CPU、主板、内存、硬盘、网卡)写入文件,求出该文件MD5值,即为该机器的硬件码

我的源码如下:


#!/bin/bash
###########################
#功能:获取硬件码
#作者:
#日期:
###########################
dmidecode="/usr/sbin/dmidecode"
smartctl="/usr/sbin/smartctl"
df="/bin/df"
lspci="/sbin/lspci"
eth_file="/etc/udev/rules.d/70-persistent-net.rules"

if [ ! -f $dmidecode ]
then
	echo "dmidecode: command not found!"
	echo "please \"yum\" to install it"
	exit
fi

path=`pwd`
tmp_file=$path"/hdid_tmp"
touch $tmp_file
> $tmp_file

#获取cpu信息
echo "CPU information:" >> $tmp_file
$dmidecode -t processor | awk '/ID|Version|Serial Number|Core Count/'| sed 's/^[ \t]*//g' >> $tmp_file

#获取BIOS信息
echo -e "\nBIOS information:" >> $tmp_file
$dmidecode -t bios | awk '/Vendor/,/Release Date/'| sed 's/^[ \t]*//g' >> $tmp_file

#获取内存信息
echo -e "\nMemory information:" >> $tmp_file
$dmidecode -t memory | awk '/Number Of Devices|^[ \t]*Size|Locator|Serial Number|Part Number/' | sed 's/^[ \t]*//g' >> $tmp_file

#获取硬盘信息
echo -e "\nDisk information:" >> $tmp_file
dev_name=`$df | grep dev | head -n 1 | awk '{print $1}'`
$smartctl -a $dev_name | awk '/Device Model/,/Firmware Version/' >> $tmp_file

#获取网卡信息
echo -e "\nEthernet information:" >> $tmp_file
$lspci | grep "Ethernet controller" >> $tmp_file
while read line
do
        put_out=`echo $line | sed -r "s/(.*)address\}==\"([a-z0-9:]{17})\",(.*)NAME=\"(.*)\"/\2 \4/g"`
        mac=`echo $put_out | awk '{print $1}'`
        if [ ${#mac} -eq 17 ]
        then
                echo $put_out >> $tmp_file
        fi
done <$eth_file

#硬件信息文件MD5值为硬件信息码
/usr/bin/md5sum -t $tmp_file | awk '{print $1}'
rm -rf $tmp_file


上一篇:快速排序算法
下一篇:c语言堆与栈及内存分配