batch file - Plink fails to run when executed from PHP in wamp, but works when executed manually

272

I need to access a Linux machine from Windows 7 via PHP.

For that I created simple bat (MyScript.bat) script containing plink.

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

When I am executing the bat script, it's working fine, i.e. the output is written in file11.txt

But when I am accessing it from PHP, the11.txt is created without data

echo exec('MyScript.bat');

Moreover, in browser, the script commands are displayed as text. I even tried to useprint_r for the display.

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

Answer

Solution:

Do not launch external tool for SSH.

PHP has native support for SSH.

Or use phpseclib:

require __DIR__ . '/vendor/autoload.php';

use phpseclib\Net\SSH2;

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

See https://phpseclib.sourceforge.net/ssh/2.0/examples.html


Anyway, if you want to use Plink, redirect also the standard error output to debug your problem:

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

See How to redirect Windows cmd stdout and stderr to a single file?.

You are for sure missing the to explicitly specify a fingerprint of the trusted hostkey.

People are also looking for solutions to the problem: php - Can I share the /uploads/ folder between multiple WordPress servers?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.