Getting hard drive serial number from USB devices

Madison Kelly linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org
Sat Jan 14 21:27:58 UTC 2006


Paul Sutton wrote:
> Thanks
> 
> paul

   No problem!

   I wanted to understand what was happening a bit better (as I said, I 
got that script working off of what I found Googling so I didn't really 
know how it worked). So I've expanded on it a bit to add comments 
explaining each step a little better. If you want to see the source data 
just 'cat' (as 'root') '/dev/ide/hdX/identify' and you will see a bunch 
of hex values.

   So here is the same script except now it takes the device from the 
command line and has a few more comments. Uncomment the 'print ...' 
statements to see what's happening as it runs, if you are curious.

-=-=-=-=-
#!/usr/bin/perl

use strict;
use warnings;
use FileHandle;

my $dev;
foreach ( @ARGV )
{
	$dev=shift;
}

if (( ! defined $dev ) || ( $dev eq "" ))
{
	die "I'm sorry but you didn't define a device.\n";
}

# Read in the 'identify' special file.
my $read_id = new FileHandle;
if ( $read_id->open("< /proc/ide/$dev/identify") )
{
	print "This appears to be an IDE disk.\n";
}
else
{
	die "I can't read any devices for: [$dev]\n";
}

<$read_id>;

# Join the end of the first line and the start of the second line from
# '<$read_id>' into a string variable.
my $hex_sn = substr(<$read_id>,10,29).substr(<$read_id>,0,20);
#print "Raw:   [$hex_sn]\n";

# Get rid of while spaces and hex space characters.
$hex_sn=~s/[2020|\ ]//g;
#print "Full:  [$hex_sn]\n";

# Break up '$hex_sn' into up to 20 2-byte arbitrary binary data chunks
# and chain the resulting 'chr/hex' values into '$serial'.
my $serial;
foreach (unpack("a2" x 20,$hex_sn))
{
	next if $_ eq "";
#	print "Split: [$_]/[".chr(hex $_)."]\n";
	$serial.=chr(hex $_);
}

# Done! Print the results and exit;
print "The serial number of device: [$dev] is: [$serial].\n";
$read_id->close;
exit(0);

-=-=-=-=-

# ./test.pl hda
This appears to be an IDE disk.
The serial number of device: [hda] is: [3LF3S4C].

-=-=-=-=-

Madison

PS - I'm still looking for a way to get the serial number from USB 
connected hard drives... :p

-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
           Madison Kelly (Digimer)
    TLE-BU; The Linux Experience, Back Up
Main Project Page:  http://tle-bu.org
Community Forum:    http://forum.tle-bu.org
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list