douxiong2001 2017-08-15 16:51
浏览 88

从远程服务器PHP执行bash脚本

I am trying to run a bash script from a remote server and output the results from the server back to my html/php page so the user can see the output. Does anyone know the best way to do this?

Here is what I have so far:

<?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>";
 }

} 
?>
  • 写回答

1条回答 默认 最新

  • dro44817 2017-08-15 17:03
    关注

    I found my answer. See the following.

               $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);
               }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3