Skip to content

Instantly share code, notes, and snippets.

@RexKang
Created April 28, 2014 00:23
Show Gist options
  • Save RexKang/11358869 to your computer and use it in GitHub Desktop.
Save RexKang/11358869 to your computer and use it in GitHub Desktop.
获得机器信息的脚本
#!/bin/bash
#
#检测是否是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 '\t│\n\t├──主板信息:\n';
# dmidecode -t 2|awk 'FS=":"{if($0~/Manufacturer/){manufacturer=$2;}if($0~/Product Name/){product=$2;}}END{print "\t│\t│\n\t│\t├──" manufacturer" --"product}'
dmidecode -t 2|awk 'FS=":"{if($0~/Manufacturer/ && $2 !~/Not Specified/){manufacturer=$2;}if($0~/Product Name/ && $2 !~/Not Specified/){product=$2;}}END{print "\t│\t│\n\t│\t├──" manufacturer" --"product;}';
echo -ne '\t│\n\t├──内存信息:\n';
# dmidecode -t 17|awk 'FS=":"{if($0 ~"Size:" && $2 !~ /No Module Installed/){g=1}}g{if($0 ~/Size:/ || $0 ~/[^ ]Speed:/ || $0 ~/Manufacturer:/){gsub(/\s/,"",$0);print "\t│\t│\n\t│\t├──"$0}}';
# midecode -t 17|awk 'FS=":"{if($0 ~"Size:" && $2 !~ /No Module Installed/){nu=NR+9;if(NR<nu){g=1}}}g{if($0 ~/Size:/ || $0 ~/[^ ]Speed:/ || $0 ~/Manufacturer:/){gsub(/\s/,"",$0);print $0}}'
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 "\t│\t│\n\t│\t├──"$0}}';
fi
echo -ne '\t│\n\t├──处理器信息:\n';
awk 'FS=": "{if($1~"model name"){print "\t│\t│\n\t│\t├──"$2}}' /proc/cpuinfo;
echo -ne '\t│\n\t├──磁盘驱动:\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 "\t│\t│\n\t│\t├──"$dev_type"\n";
echo -ne "\t│\t\t│\n\t│\t\t├──"$dev"\n";
else
echo -ne "\t│\t│\n\t│\t├──"$dev"\n";
fi
done
echo -ne '\t│\n\t├──网络适配器:\n';
lspci|awk 'FS=":"{if($0~"Ethernet controller"){print "\t│\t│\n\t│\t├──"$3}}'
echo
if [ $smart == 'null' -o $dmi == 'null' ]
then
echo '如果要获得更详细信息请安装"smartmontools"和"dmidecode"工具~yum安装方法是"yum install smartmontools dmidecode"'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment