php/dos :如何解析 regedit 导出文件?
我的目标是在注册表配置单元中查找 Company 键值,然后提取相应的 Guid 以及其后面的其他键和值。 所以我想我应该运行 regedit export 命令,然后使用 php 解析文件以获取我需要的密钥。
因此,运行 dos 批处理命令后,
>regedit /E "output.txt" "HKLM\System....\Company1"
输出文本文件似乎采用某种 UNICODE 格式,这对正则表达式不友好。 我正在使用 php 解析文件并提取密钥。
这是我用来解析文件的 php 代码
<?php
$regfile = "output.txt";
$handle = fopen ("c:\\\\" . $regfile,"r");
//echo "handle: " . $file . "<br>";
$row = 1;
while ((($data = fgets($handle, 1024)) !== FALSE) ) {
$num = count($data);
echo "$num fields in line $row: \n";
$reg_section = $data;
//$reg_section = "[HKEY_LOCAL_MACHINE\SOFTWARE\TECHNOLOGIES\MEDIUS\CONFIG MANAGER\SYSTEM\COMPANIES\RECORD11]";
$pattern = "/^(\[HKEY_LOCAL_MACHINE\\\SOFTWARE\\\TECHNOLOGIES\\\MEDIUS\\\CONFIG MANAGER\\\SYSTEM\\\COMPANIES\\\RECORD(\d+)\])$/";
if ( preg_match($pattern, $reg_section )) {
echo "<font color=red>Found</font><br>";
} else {
echo "not found<br>";
echo $data . "<br>";
}
$row++;
} //end while
fclose($handle);
?>
,输出如下所示......
第 1 行有 1 个字段:未找到 ÿþW�i�n�d�ow�s�R�e�g�i�t�r�y� “编辑”“查看” �5�.�0�0� � 第 2 行 1 个字段:否 找到了
第 3 行有 1 个字段:未找到 [�H�K�E�Y��L�O�C�A�L��M�A�C�H�I�N�E�\�S�O�F �T�W�A�R�E�\�I�N�T�E�R�S�T�A�R� �T�E�C�H�N�O�L�O�G�I�E�S�\�X�M�E�D�I�U�S�\�C�O�N�F �I�G� �M�A�N�A�G�E�R�\�S�Y�S�T�E�M�\�C�O�M�P�A�N�I�E�S�] � � 第 4 行 1 个字段:未找到 “……下一步……重新接收……” �I�D�"�=�"�4�1�"� � 1 行字段 5:未找到
任何想法如何解决这个问题?
提前致谢
My objective is to look for Company key-value in the registry hive and then pull the corresponding Guid and other keys and values following it. So I figured i would run the regedit export command and then parse the file with php for the keys I need.
So after running the dos batch command
>regedit /E "output.txt" "HKLM\System....\Company1"
The output textfile seems to be in some kind of UNICODE format which isn't regex friendly. I'm using php to parse the file and pull the keys.
Here is the php code i'm using to parse the file
<?php
$regfile = "output.txt";
$handle = fopen ("c:\\\\" . $regfile,"r");
//echo "handle: " . $file . "<br>";
$row = 1;
while ((($data = fgets($handle, 1024)) !== FALSE) ) {
$num = count($data);
echo "$num fields in line $row: \n";
$reg_section = $data;
//$reg_section = "[HKEY_LOCAL_MACHINE\SOFTWARE\TECHNOLOGIES\MEDIUS\CONFIG MANAGER\SYSTEM\COMPANIES\RECORD11]";
$pattern = "/^(\[HKEY_LOCAL_MACHINE\\\SOFTWARE\\\TECHNOLOGIES\\\MEDIUS\\\CONFIG MANAGER\\\SYSTEM\\\COMPANIES\\\RECORD(\d+)\])$/";
if ( preg_match($pattern, $reg_section )) {
echo "<font color=red>Found</font><br>";
} else {
echo "not found<br>";
echo $data . "<br>";
}
$row++;
} //end while
fclose($handle);
?>
and the output looks like this....
1 fields in line 1: not found
ÿþW�i�n�d�o�w�s� �R�e�g�i�s�t�r�y�
�E�d�i�t�o�r� �V�e�r�s�i�o�n�
�5�.�0�0� � 1 fields in line 2: not
found1 fields in line 3: not found
[�H�K�E�Y��L�O�C�A�L��M�A�C�H�I�N�E�\�S�O�F�T�W�A�R�E�\�I�N�T�E�R�S�T�A�R�
�T�E�C�H�N�O�L�O�G�I�E�S�\�X�M�E�D�I�U�S�\�C�O�N�F�I�G�
�M�A�N�A�G�E�R�\�S�Y�S�T�E�M�\�C�O�M�P�A�N�I�E�S�]�
� 1 fields in line 4: not found
"�N�e�x�t� �R�e�c�o�r�d�
�I�D�"�=�"�4�1�"� � 1 fields in line
5: not found
Any ideas how to approach this?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将 /A 添加到 REGEDIT 命令中,如下所示以生成兼容的输出:
REGEDIT /E /A "output.txt" "HKEY_LOCAL_MACHINE\System....\Company1"
Try adding /A to REGEDIT command like this to produce compatible output:
REGEDIT /E /A "output.txt" "HKEY_LOCAL_MACHINE\System....\Company1"
我知道有一个 Perl 库:
Parse::Win32Registry
不过,它的课程应该不会太难。 PHP 还有一个 PECL 扩展,可以解析 Perl 代码:
http://devzone.zend .com/node/view/id/1712
I know there is a Perl library for this:
Parse::Win32Registry
Making a PHP class from it shouldn't be too difficult though. There's also a PECL extension for PHP that will parse Perl code:
http://devzone.zend.com/node/view/id/1712
正则表达式与 unicode 配合得很好。 您收到具体的错误消息吗?
Regular expressions work fine with unicode. Are you getting a specific error message?
从 Windows XP 开始,Regedit 导出为 Unicode,因此为 2 个字节。 如果您在记事本中打开导出,您会看到这一点。 我不确定旧版本的 php 是否能够处理 unicode 文件。
有没有办法读取您需要的特定密钥? 通过另一个工具等。这将是一个更直接的方法。
From Windows XP the Regedit export is Unicode and therefore 2 bytes. You'll see this if you open up the export in notepad. I'm not sure older versions of php are able to handle unicode files.
Is there no way you can read the specific key you need? Through another tool etc. That would be a much more straighforward approach.