2017-04-09 142 views
0

我有我的前端有一个按钮,这个按钮说话后端。 后端开始远程脚本:PHP:SSH连接和python脚本执行

<?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); 
?> 

我有2个问题,首先一个,PHP应等待的python脚本来完成,因为它说here但事实并非如此。

第二个,它给了我下面的:

PHP的警告:FWRITE()预计参数2为字符串,资源上线41 /var/www/html/WEBAPP/wa_start.php给出

+0

放输出;'FWRITE这里 –

回答

1

使用stream_get_contents(stream_out);

在你的代码$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);这将返回resource不串输出。试试这个代码。 `的后续代码var_dump($ stream_out)

$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); 
+0

前@MouIdri请检查'$ stream_out'将从'$ stream_out = ssh2_fetch_stream($流,SSH2_STREAM_STDIO)接收;' –

+0

@Mouldri你只需要用我的代码替换代码的最后三行。 –

+0

对不起,我没有看到缺少的$ ..我改变了它,我没有任何错误信息了。但是行为非常奇怪,因为没有任何东西被写入文件(我chrome 0777,所以它不是一个权利问题)。 – MouIdri