PHP:使用密码通过管道运行shell命令

问题描述 投票:0回答:1

我需要运行此命令来打开一个Mac加密的图像,我忘记了密码,并且希望通过代码来强行使用。

只是检查我的代码是否正常工作,我创建了一个新图像进行检查。它不起作用。

$command = "echo -n Secure@3@1| hdiutil attach -stdinpass Hidden.dmg"

// I have tried exec, shell_exec, popen like
exec($command);

// Every time it returns `hdiutil: attach failed - Authentication error`

有人可以建议代码出什么问题,因为我已经从shell本身验证了密码正确并且命令有效。

php command-line-interface exec shell-exec
1个回答
0
投票
https://www.php.net/manual/en/function.system.php#94929复制了此功能。

function my_exec($cmd, $input='') { $proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes); fwrite($pipes[0], $input);fclose($pipes[0]); $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]); $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]); $rtn=proc_close($proc); return array('stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$rtn ); } my_exec('hdiutil attach -stdinpass Hidden.dmg', 'Secure@3@1'); // Worked

调用此函数无需使用管道即可。

仍在寻找答案,为什么管道操作员不起作用。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.