如何在python中获取硬盘驱动程序的序列号 [英] how to get hard disk driver's serial number in python

查看:89
本文介绍了如何在python中获取硬盘驱动程序的序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 wmi 来获取硬盘的序列号.我所做的是这样的:

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

启动ipython,使用这种方式获取序列号:

Start ipython and use this way to get serial num:

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

结果是这样的:

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

但是我的电脑是win7,我用管理员权限启动ipython,再次做同样的事情,但是现在结果不一样了:

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";
};

我猜第二个结果更像是一个正确的结果.那么有人可以告诉我在Windows上获取序列号的正确方法吗,包括XP,Vista,Win7,Win8?

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?

我发现很多人使用 CreateFileA 和 DeviceIoControl 来获取序列号.

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

推荐答案

如果你在谷歌Win32_PhysicalMedia",第二次命中是一个错误报告,现在我更仔细地阅读了所有内容,看起来它准确地描述了你的问题(但没有提供从我看到的修复):http://connect.microsoft.com/VisualStudio/feedback/details/623282/win32-physicalmedia-returns-incorrect-serial-number-on-vista-或更高时以标准用户身份运行

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:

看看那两个字符串:

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

请注意,第一个似乎是一个十六进制数.0x20 是一个空格字符.所以第一个有很多空格,后面跟着几个字节的真实数据,这使得两个序列号的大小相当.

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.

现在,使用十六进制到 ASCII 转换器,例如 http://www.dolcevie.com/js/converter.html 并输入第一个数字.你得到:

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).

要一劳永逸地解决这个问题,您需要告诉我们您认为这三个中哪一个是序列号的正确"表示(理想情况下它将与驱动器上打印的内容相匹配).一旦你弄清楚你在哪里(显然取决于平台——可能取决于你正在使用的 Python、WMI 或 Python WMI 模块的版本),在这三种表示之间进行转换就足够简单了.

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).

这篇关于如何在python中获取硬盘驱动程序的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆