简体   繁体   English

PHP:SSH连接和python脚本执行

[英]PHP: SSH connection and python script execution

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. 我有2个问题,首先一个,PHP应等待的python脚本来完成,因为它说, 在这里 ,但事实并非如此。

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 PHP警告:fwrite()期望参数2为字符串,在第41行的/var/www/html/WEBAPP/wa_start.php中给出的资源

use stream_get_contents(stream_out); 使用stream_get_contents(stream_out);

In your code $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); 在您的代码$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); this will return resource not string output. 这将返回resource而不是字符串输出。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM