How to get the serial number of a hard drive driver in python

I tried using wmi to get the serial number of the hard drive. I did the following:

Start ipython and use this method to get the serial number:

import wmi
c = wmi.WMI()
for x in c.Win32_PhysicalMedia():
    print x

      

The result looks like this:

instance of Win32_PhysicalMedia
{
    SerialNumber = "2020202020202020202020205635514d385a5856";
    Tag = "\\\\.\\PHYSICALDRIVE0";
};

      

But my computer is win7, I run ipython with admin rights and do the same again, but now the result is different:

instance of Win32_PhysicalMedia
{
    SerialNumber = "            5VMQZ8VX";
    Tag = "\\\\.\\PHYSICALDRIVE0";
};

      

I assume the second result looks more like the correct result. So can anyone show me the correct way to get the serial number on windows including XP, Vista, Win7, Win8?

I found that a lot of people use CreateFileA and DeviceIoControl to get the serial number.

+3


source to share


2 answers


If you google "Win32_PhysicalMedia" the second hit is the bug report, which now, as I read more and more carefully, looks like it accurately describes your problem (but doesn't offer any fixes from what I see): http : //connect.microsoft.com/VisualStudio/feedback/details/623282/win32-physicalmedia-returns-incorrect-serial-number-on-vista-or-higher-when-run-as-standard-user

Therefore, you may have to take matters into your own hands. This is what I wrote in this answer originally:

Take a look at these two lines:

2020202020202020202020205635514d385a5856
 5VMQZ8VX (yes there a space in front)

      

Note that the hexadecimal number is presented first. 0x20 is a space character. Thus, the former has many spaces followed by several bytes of real data, making the two serial numbers comparable in size.



Now use a Hex-to-ASCII converter like http://www.dolcevie.com/js/converter.html and click on the first number. You get:

            V5QM8ZXV

      

See how it looks like? The only difference now is the endianness.

To solve this problem once and for all, you need to tell us which of the three you think is the "correct" representation of the serial number (ideally this will match what is printed on the disk). It will be easy enough to convert between the three views when you figure out where you are (depending on the platform - maybe the version of Python, WMI, or the WMI Python module you are using).

+3


source


>>> import binhex
>>> binascii.a2b_hex("2020202020202020202020205635514d385a5856")
'            V5QM8ZXV'

      



alternating characters are swapped ... looks like they are probably the same serial number.

0


source







All Articles