gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

Exec.php (1141B)


      1 <?php
      2 
      3 namespace Ssh;
      4 
      5 use RuntimeException;
      6 
      7 /**
      8  * Wrapper for ssh2_exec
      9  *
     10  * @author Cam Spiers <camspiers@gmail.com>
     11  * @author Greg Militello <junk@thinkof.net>
     12  * @author Gildas Quéméner <gildas.quemener@gmail.com>
     13  */
     14 class Exec extends Subsystem
     15 {
     16     protected function createResource()
     17     {
     18         $this->resource = $this->getSessionResource();
     19     }
     20 
     21     public function run($cmd, $pty = null, array $env = array(), $width = 80, $height = 25, $width_height_type = SSH2_TERM_UNIT_CHARS)
     22     {
     23         $cmd .= ';echo -ne "[return_code:$?]"';
     24         $stdout = ssh2_exec($this->getResource(), $cmd, $pty, $env, $width, $height, $width_height_type);
     25         $stderr = ssh2_fetch_stream($stdout, SSH2_STREAM_STDERR);
     26         stream_set_blocking($stderr, true);
     27         stream_set_blocking($stdout, true);
     28 
     29         $output = stream_get_contents($stdout);
     30         preg_match('/\[return_code:(.*?)\]/', $output, $match);
     31         if ((int) $match[1] !== 0) {
     32             throw new RuntimeException(stream_get_contents($stderr), (int) $match[1]);
     33         }
     34 
     35         return preg_replace('/\[return_code:(.*?)\]/', '', $output);
     36     }
     37 }