php 通过PHP获取厂商提供的硬盘序列号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/709108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:35:21  来源:igfitidea点击:

Getting Serial Number of the Hard Drive Provided by the manufacturer through PHP

phphard-driveserial-number

提问by simplfuzz

Getting Serial Number of the Hard Drive Provided by the manufacturer through PHP : How can it be done? I want to store it in a file.

通过PHP获取厂商提供的硬盘序列号:怎么做?我想将它存储在一个文件中。

OS : windows 2000,XP,ME,Vista...

操作系统:windows 2000,XP,ME,Vista...

Yes, I want the serial number of the hard drive of the Server.

是的,我想要服务器硬盘的序列号

Or can it be done through Adobe AIR? Or can it be done through a C program on Windows?

还是可以通过 Adob​​e AIR 完成?还是可以通过 Windows 上的 C 程序完成?

C:\Documents and Settings\Administrator>dir
 Volume in drive C has no label.
 Volume Serial Number is BC16-5D5F

Is this number : BC16-5d5f unique for a hard drive? How is it different from the manufacturer given serial number?

这个数字 : BC16-5d5f 对于硬盘驱动器是唯一的吗?它与制造商给出的序列号有何不同?

wmic DISKDRIVE GET SerialNumber

Displays only the following text on my Vista Machine:

在我的 Vista 机器上仅显示以下文本:

SerialNumber

序列号

On my XP machine, the command is unrecognized.

在我的 XP 机器上,无法识别该命令。

回答by OIS

The following returns the disk serial number. Should work with multiple drives, you'll just get multiple results. Just run it with shell_exec.

下面返回磁盘序列号。应该使用多个驱动器,你只会得到多个结果。只需使用shell_exec运行它。

wmic DISKDRIVE GET SerialNumber

wmic.exe is located in your windows system32 folder. And wmic does exist on WinXP, Ive used it there myself.

wmic.exe 位于您的 windows system32 文件夹中。wmic 确实存在于 WinXP 上,我自己在那里使用过它。

My result on Vista:

我在 Vista 上的结果:

C:\Windows\System32>wmic DISKDRIVE GET SerialNumber
SerialNumber
20202020202054534241354c4*snip*

I do not know if all harddrives provides the serial number to the OS.

我不知道是否所有硬盘都向操作系统提供了序列号。

It seems the wmic command is only available on the professional versions of Windows XP, Windows Vista and Windows 7.

wmic 命令似乎只适用于专业版的 Windows XP、Windows Vista 和 Windows 7。

回答by Patrick Glandien

PHP itself has no way of accessing the hardware like that.

PHP 本身无法像那样访问硬件。

You will have to either

你必须要么

  • use a command of your operating system and call it with system()or exec()
  • write an extension for PHP that will return you the information
  • 使用操作系统的命令并使用system()或调用它exec()
  • 为 PHP 编写一个扩展,它将返回信息

If you are on Linux and have the necessary privileges and configuration you can use $r = system("hdparm -I /dev/hda");(replace hda with your hd) to get the serial number of a given hard drive.

如果您使用的是 Linux 并且拥有必要的权限和配置,您可以使用$r = system("hdparm -I /dev/hda");(用您的 hd 替换 hda)来获取给定硬盘驱动器的序列号。

回答by Andrei Serdeliuc ?

hdparm -i /dev/sdX

that's on linux, not sure on windows though. You could execute that via "system()"

那是在 linux 上,但不确定在 windows 上。你可以通过“system()”来执行

Have a look at http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.hk.msdn.connection&tid=e41f0af2-2e76-4be6-9b7b-636e79ac0491&cat=zh_HK_3b03d742-993a-4f96-accd-1063c6bfd559&lang=zh&cr=HK&sloc=&p=1

看看http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.hk.msdn.connection&tid=e41f0af2-2e76-4be6-9b7b-636e79ac0491&cat=zh_HK_3b03d742- 993a-4f96-accd-1063c6bfd559&lang=zh&cr=HK&sloc=&p=1

Might be a way forward.

可能是前进的方向。

Also, when I ran a "dir" on the command prompt, it shows:

另外,当我在命令提示符下运行“dir”时,它显示:

C:\Documents and Settings\Administrator>dir
 Volume in drive C has no label.
 Volume Serial Number is BC16-5D5F

Is that what you're looking for?

这就是你要找的吗?

回答by Bart van Heukelom

I can't tell you the answer, but I guess you'll have to look in the direction of extensions (maybe even writing one yourself). I doubt this is something PHP's core has.

我不能告诉你答案,但我想你必须看看扩展的方向(甚至可能自己写一个)。我怀疑这是 PHP 的核心所拥有的东西。

Edit: I forgot about the raw power of "exec" :-/

编辑:我忘记了“exec”的原始力量:-/

回答by Dustin G

Run the following with shell_exec(test in command prompt if needed):

使用shell_exec运行以下命令(如果需要,请在命令提示符下测试):

wmic path win32_physicalmedia get Tag,SerialNumber

DISKDRIVE doesn't get the actual serial number for my drive that shows plugged in through an IDE channel. the above seemed to get the actual serial numbers for all of my drives. Tag will also return you what type of drive it is which may be helpful for identifying different drives.

DISKDRIVE 没有获得显示通过 IDE 通道插入的驱动器的实际序列号。以上似乎获得了我所有驱动器的实际序列号。标签还将返回您的驱动器类型,这可能有助于识别不同的驱动器。

Example output:

示例输出:

SerialNumber     Tag
WD-WX55D33JQNZ4  \.\PHYSICALDRIVE4
S1OKIJNH938475   \.\PHYSICALDRIVE0
WD-CV44HJ5L765Y  \.\PHYSICALDRIVE1
WD-WX41D65SD1UU  \.\PHYSICALDRIVE2
WD-WXB1SD3OIJHG  \.\PHYSICALDRIVE3
                 \.\CDROM0

回答by MrHus

Do you want the hard drive from the server or a client? PHP runs on the server so getting it straight from the client doens't seem possible to me.

您想要来自服务器还是客户端的硬盘驱动器?PHP 运行在服务器上,所以直接从客户端获取它对我来说似乎是不可能的。

The manual suggest you can execute commands on you server: http://nl2.php.net/manual/en/ref.exec.php

手册建议您可以在服务器上执行命令:http: //nl2.php.net/manual/en/ref.exec.php

Unfortunately I don't enough Unix to get you hdd serials.

不幸的是,我没有足够的 Unix 来为您提供硬盘序列。

回答by Lenin

You can use

您可以使用

    $hdserial =`wmic DISKDRIVE GET SerialNumber 2>&1` 

or

或者

    $hdserial =`wmic bios get serialnumber 2>&1` 

Then you can echo it.

然后你就可以回声了。

Based on Patrick Daryll Glandien's hint, you can execute following on *nix based machines. $hdserial= hdparm -I /dev/hda

根据Patrick Daryll Glandien的提示,您可以在基于 *nix 的机器上执行以下操作。$hdserial=hdparm -I /dev/hda

hdparm -i /dev/sdareturns lesser info. But as hdparm needs root access, it did not run with php for me.

hdparm -i /dev/sda返回较少的信息。但是由于 hdparm 需要 root 访问权限,所以它没有与 php 一起运行。

The '2>&1' part is used from the suggestion here.

'2>&1' 部分是从这里建议中使用的。

回答by Veeren Sharma

Try this code it's working properly.

试试这个代码它工作正常。

<?php
   function GetVolumeLabel($drive) {
       // Try to grab the volume name
       if (preg_match('#Volume Serial Number is (.*)\n#i', shell_exec('dir '.$drive.':'), $m)) {
          $volname = ' ('.$m[1].')';
       } else {
           $volname = '';
       }
   return $volname;
}

$serial = str_replace("(","",str_replace(")","",GetVolumeLabel("c")));
echo $serial;

?>

回答by Fery Wardiyanto

On *nix based machine you can also use ls /dev/disk/by-id/because hdparmneed root permission (see Patrick Daryll G.answer).

在基于 *nix 的机器上,您也可以使用,ls /dev/disk/by-id/因为hdparm需要 root 权限(请参阅Patrick Daryll G.回答)。

<?php
exec($command.' 2>&1', $output);
echo 'HDD: '.$output[0].'<br>';

$outputs = explode('_', $outputs[0]);
$outputs = end($outputs);
echo 'HDD-SN: '.$output.'<br>';

and you will get something like this

你会得到这样的东西

HDD: ata-HGST_XXX1234567890XX_ABCD123456789X  // <connection>-<hdd_model>_<hdd_sn>
HDD-SN: ABCD123456789X  // Your HDD Serial Number