0

I have my front end with a button, this button talks to backend. The backend is starting a remote script :

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$connection = ssh2_connect('192.168.56.180', 22);
ssh2_auth_password($connection, 'root', 'password');
$stream = ssh2_exec($connection, 'python /WATSON/APP/test/testlistrbk.py');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo $stream_out_contente;
fwrite($myfile, $stream_out);
fclose($myfile);
?> 

I have 2 issues , First one, php should wait the python script to finish as it said here but it does not.

Second one, It gives me the following :

PHP Warning: fwrite() expects parameter 2 to be string, resource given in /var/www/html/WEBAPP/wa_start.php on line 41

Community
  • 1
  • 1
MouIdri
  • 1,300
  • 1
  • 18
  • 37

1 Answers1

1

use stream_get_contents(stream_out);

In your code $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); this will return resource not string output. Try this code.

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");

$connection = ssh2_connect('192.168.56.180', 22);
ssh2_auth_password($connection, 'root', 'password');
$stream = ssh2_exec($connection, 'python /WATSON/APP/test/testlistrbk.py');

stream_set_blocking($stream, true);

$outputStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);

echo "Output: " . stream_get_contents($outputStream);
echo "Error: " . stream_get_contents($errorStream);

fwrite($myfile, $outputStream.$errorStream);
fclose($myfile);
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • @MouIdri Please check `$stream_out` will be received from `$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);` – Sahil Gulati Apr 09 '17 at 13:17
  • @Mouldri you just need to replace last three lines of your code with my code. – Sahil Gulati Apr 09 '17 at 13:18
  • Sorry, I did not see the missing $.. I changed it and I do not have error message any more,. But behavior is very strange, because nothing is written to file ( I chmoded 0777, so it is not a rights issue ). – MouIdri Apr 09 '17 at 13:19
  • @MouIdri i am updating my post then you can check – Sahil Gulati Apr 09 '17 at 13:19
  • @MouIdri Try this code and check whether it is working fine or not You can refer to this as well http://php.net/manual/en/function.ssh2-exec.php#99089 – Sahil Gulati Apr 09 '17 at 13:26