For hardware inventory purpose we need to find out hard disk serial number, model number & size. To make the task simpler you can use hdparm or lshw. You can use following scripts to find out hard disk serial number, model number & size

1. Using hdparm

vi /tmp/hdparm_hdd

Paste the following content in /tmp/hdparm_hdd

#!/bin/sh -x
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
FROOT=`id | grep "uid=" | gawk '{print $1}' | cut -d "(" -f1 | sed 's/uid=//g'`
if [ $FROOT -ne 0 ]
        then
                echo "Please run the script as a root user"
        else
	rm -rf /tmp/hdd0
	rm -rf /tmp/hdd
	fdisk -l | grep "Disk" | grep "dev" | cut -d ":" -f1| sed 's/Disk //g' > /tmp/hdd
		for hdd in `cat /tmp/hdd`
		do
			hdparm -i $hdd | egrep "Model|SerialNo" | sed 's/,/\n/g' | sed 's/^ //g' | egrep "Model|SerialNo" >> /tmp/hdd0
			echo "Size = `fdisk -l $hdd | grep "Disk" | grep "dev" | sed 's/^Disk //g' | cut -d "," -f1 | cut -d ":" -f2 | sed 's/^ //g'`" >> /tmp/hdd0
			echo "" >> /tmp/hdd0
		done
	clear
	CT=`cat /tmp/hdd | wc -l`
	echo "You have $CT HardDisk/s"
	echo -e "They are \n`cat /tmp/hdd0`"
fi
chmod +x /tmp/hdparm_hdd
/tmp/hdparm_hdd

2. Using lshw
First you need to install lshw

yum install -y lshw
vi /tmp/lshw_hdd

Paste the following content in /tmp/lshw_hdd

#!/bin/sh -x
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
FROOT=`id | grep "uid=" | gawk '{print $1}' | cut -d "(" -f1 | sed 's/uid=//g'`
if [ $FROOT -ne 0 ]
        then
                echo "Please run the script as a root user"
        else
		MYTTY=`tty`
		lshw > /tmp/hdd
		HDDCOUNT=`cat /tmp/hdd | grep -A10 "*-disk" | egrep "product|serial|size" | grep "product" | wc -l`
		#HDD=`cat /tmp/hdd | grep -A10 "*-disk" | egrep "product|serial|size" | sed 's/^[ \t]*//g' | sed '/size/s/$/ BUNTY/g' | tr -d '\n' | sed 's/BUNTY/\n/g' | sed 's/product:/ /g;s/serial:/ /g;s/size:/ /g' | sed 's/^  //g'`
		clear
		HDD=`cat /tmp/hdd | grep -A10 "*-disk" | egrep "product|serial|size" | sed 's/^[ \t]*//g' | sed '/GB/G'`
		echo "YOU HAVE $HDDCOUNT HARD DISK, Following are the details"
		echo "$HDD"
fi
chmod +x /tmp/lshw_hdd
/tmp/lshw_hdd
Hard Disk Serial Number in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *