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

1
  • put output of var_dump($stream_out); before fwrite here Commented Apr 9, 2017 at 13:10

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

@MouIdri Please check $stream_out will be received from $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
@Mouldri you just need to replace last three lines of your code with my code.
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 i am updating my post then you can check
@MouIdri Try this code and check whether it is working fine or not You can refer to this as well php.net/manual/en/function.ssh2-exec.php#99089

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.