2017-04-09 51 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