我正在尝试从远程服务器运行bash脚本,并将结果从服务器输出回html/php页面,以便用户可以看到输出。有人知道做这件事的最好方法吗?
这是我到目前为止所知道的:
<?php
include 'res/php/functions.php';
$gateway = 'remoteserver';
$user = 'user';
$pass = 'pass';
function cleanInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//when the submit button is clicked
if ( $_SERVER['REQUEST_METHOD'] == 'POST'){
//$gateway = cleanInput($_POST['gateway']);
//create the ssh connection
if ($connection = @ssh2_connect($gateway, 22)) {
ssh2_auth_password($connection, $user, $pass);
if(isset($_POST['option']) && $_POST['option'] == 1) {
$output =
shell_exec("/tmp/testscripts/up.sh");
}
if(isset($_POST['option']) && $_POST['option'] == 2) {
$output =
shell_exec("/tmp/testscripts/down.sh");
}
//remove this if you want to see $output in this file
echo "<pre>$output</pre>";
}
}
?>发布于 2017-08-16 01:03:13
我找到了答案。请参阅以下内容。
$stream = ssh2_exec($connection, "/tmp/moslehpour/testscripts/up.sh");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);
}
if(isset($_POST['option']) && $_POST['option'] == 2) {
$stream = ssh2_exec($connection, "/tmp/moslehpour/testscripts/down.sh");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);
}https://stackoverflow.com/questions/45697618
复制相似问题