Windows | wamp |无法从php exec()执行plink.exe

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

我需要通过PHP从Windows 7访问Linux机器。

为此,我创建了包含plink的简单bat(MyScript.bat)脚本。

c:\wamp\www\abc\plink.exe [email protected] -pw l1c -C "df -h">11.txt

当我执行bat脚本时,它工作正常,即输出写在文件11.txt

但是当我从PHP访问它时,11.txt是在没有数据的情况下创建的

echo exec('MyScript.bat');

此外,在浏览器中,脚本命令显示为文本。我甚至尝试使用print_r进行显示。

"c:\wamp\www\abc\plink.exe [email protected] -pw l1c -C "df -h">11.txt
php batch-file wamp exec plink
1个回答
0
投票

不要为SSH启动外部工具。

PHP有native support for SSH

或者使用phpseclib

require_once("Net/SSH2.php");
require_once("Crypt/RSA.php");

$ssh = new Net_SSH2($hostname);
if ($ssh->login($username, $password))
{
    echo $ssh->exec("df -h");
}

http://phpseclib.sourceforge.net/ssh/examples.html


无论如何,如果你想使用Plink,还要重定向标准错误输出来调试你的问题:

plink.exe .. dir > 11.txt 2>&1

Redirect Windows cmd stdout and stderr to a single file

您肯定错过了-hostkey switch来明确指定可信主机密钥的指纹。

© www.soinside.com 2019 - 2024. All rights reserved.