WARNING: failed to open pid_file '/var/run/sphinx/searchd.pid'. WARNING: indices NOT rotated.
$ip = 'ip_address'; $user = 'username'; $pass = 'password'; $connection = ssh2_connect($ip); ssh2_auth_password($connection,$user,$pass); $shell = ssh2_shell($connection,"bash"); //Trick is in the start and end echos which can be executed in both *nix and windows systems. //Do add 'cmd /C' to the start of $cmd if on a windows system. $cmd = "echo '[start]';your commands here;echo '[end]'"; $output = user_exec($shell,$cmd); fclose($shell); function user_exec($shell,$cmd) { fwrite($shell,$cmd . "\n"); $output = ""; $start = false; $start_time = time(); $max_time = 2; //time in seconds while(((time()-$start_time) < $max_time)) { $line = fgets($shell); if(!strstr($line,$cmd)) { if(preg_match('/\[start\]/',$line)) { $start = true; }elseif(preg_match('/\[end\]/',$line)) { return $output; }elseif($start){ $output[] = $line; } } } }
preg_match('/^\[start\]\s*$/',$line)
<?php class SshException extends CException {} /** * Class Ssh * It is a base class for the simplify a ssh connection management * and related commands execution * * @author Ivanenko Vladyslav */ class Ssh { const EXEC_TYPE_EXEC = 'exec'; // type for ssh2_exec() const EXEC_TYPE_SHELL = 'shell'; // type for ssh2_shell() const START_MARK = '__start__'; const FINISH_MARK = '__finish__'; const MAX_EXECUTION_TIME = 1800; // max script execution time in sec private $user; private $password; private $host; private $port; private $shellType = 'bash'; // shell type private $shell = null; //shell identificator private $ssh = null; //connection private $execType; /** * Construct * * @param null $user * @param null $password * @param null $host */ public function __construct($user = null, $password = null, $host = null, $port = null) { $config = Yii::app()->params['ssh']; $params = array('user', 'password', 'host', 'port'); foreach($params as $param) { if(isset(${$param}) && !is_null(${$param})) { $this->{$param} = ${$param}; } else { $this->{$param} = @$config[$param]; } } return true; } /** * Connect to Ssh * * @return resource * @throws SshException */ public function connect() { $this->ssh = @ssh2_connect($this->host, $this->port); if(empty($this->ssh)) { throw new SshException('Cant connect to ssh'); } if(empty($this->execType)) { $this->execType = self::EXEC_TYPE_SHELL; } return $this->ssh; } /** * Login to ssh * * @throws SshException * @return bool */ public function login() { if(!@ssh2_auth_password($this->ssh, $this->user, $this->password)) { throw new SshException('Cant login by ssh'); } return true; } /** * Exec command by ssh * * @param $cmd * @param $type * * @return string * @throws SshException */ public function exec($cmd, $type = self::EXEC_TYPE_SHELL) { if(is_null($this->ssh)) { $this->connect(); $this->login(); } $this->execType = $type; switch($this->execType) { case self::EXEC_TYPE_EXEC: $result = $this->execCommand($cmd); break; case self::EXEC_TYPE_SHELL: $result = $this->execByShell($cmd); break; default: throw new SshException('Incorrect exec type'); break; } return $result; } /** * Executes command by the direct ssh2_exec * * @param $command * * @return string * @throws SshException */ private function execCommand($command) { if (!($stream = ssh2_exec($this->ssh, $command))) { throw new SshException('Ssh command failed'); } stream_set_blocking($stream, true); $data = ""; while ($buf = fread($stream, 4096)) { $data .= $buf; } fclose($stream); return $data; } /** * Executes command within the shell opening * * @param $command * * @return string */ private function execByShell($command) { $this->openShell(); return $this->writeShell($command); } /** * opens shell * * @throws SshException */ private function openShell() { if(is_null($this->shell)) { // here is hardcoded width and height, you can change them. $this->shell = @ssh2_shell($this->ssh, $this->shellType, null, 80, 40, SSH2_TERM_UNIT_CHARS); } if( !$this->shell ) { throw new SshException('SSH shell command failed'); } } /** * * Write the command to the open shell * * @param $cmd * @param int $maxExecTime in sec * * @return string */ private function writeShell($cmd, $maxExecTime = self::MAX_EXECUTION_TIME) { // write start marker fwrite($this->shell, $this->getMarker(self::START_MARK)); // write command fwrite($this->shell, $cmd . PHP_EOL); // write end marker fwrite($this->shell, $this->getMarker(self::FINISH_MARK)); stream_set_blocking($this->shell, true); sleep(1); $output = ""; $start = false; // define the time until the script can be executed $timeUntil = time() + $maxExecTime; while(true) { if(time() > $timeUntil) { break; } $line = fgets($this->shell, 4096); // if any delay is happened while command is processing if(!is_string($line)) { sleep(1); continue; } // define the start executed command if(preg_match('/^' . self::START_MARK . '\s*$/', $line)) { $start = true; } elseif(preg_match('/^' . self::FINISH_MARK . '\s*$/', $line)) { // define the last executed command break; } elseif($start) { // add console output to the script output data $output .= $line; } } return $output; } /** * Disconnect from ssh */ public function disconnect() { $this->exec('exit'); $this->ssh = null; if(!is_null($this->shell)) { fclose($this->shell); } } /** * Disconnect in destruct */ public function __destruct() { $this->disconnect(); } /** * Returns marker command * * @param string $type * * @return string */ private function getMarker($type = self::START_MARK) { return 'echo "' . $type . '"' . PHP_EOL; } }
Source: https://habr.com/ru/post/225979/
All Articles