how to get hard disk driver's serial number in python

最后都变了- 提交于 2019-12-22 09:48:46

问题


I tried to use wmi to get the hard disk's serial number. What I did is this:

Start ipython and use this way to get serial num:

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

The result is like this:

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

But my computer is win7, I start ipython with administrator privilege and do the same thing again, but now the result is different:

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

I guess the second result is more like a correct result. So can some one show me a correct way to get the serial number on windows, include XP, Vista, Win7, Win8?

I found that many people use CreateFileA and DeviceIoControl to get the serial number.


回答1:


If you Google "Win32_PhysicalMedia", the second hit is a bug report which now that I read everything more carefully looks like it describes exactly your problem (but offers no fix 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

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

Look at those two strings:

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

Notice that the first appears to be a hexadecimal number. 0x20 is a space character. So the first one has many spaces followed by a few bytes of real data, which makes the two serial numbers comparable in size.

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

            V5QM8ZXV

See how similar that looks? The only difference now is byte ordering (endianness).

To settle this once and for all, you need to tell us which of the three you think is the "proper" representation of the serial number (ideally it will match what's printed on the drive). It will be simple enough to convert between the three representations once you figure out where you are (depends on the platform apparently--possibly on the version of Python, WMI, or the Python WMI module you're using).




回答2:


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

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



来源:https://stackoverflow.com/questions/9683278/how-to-get-hard-disk-drivers-serial-number-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!