phpseclib\Net\SSH2::read PHP Method

read() public method

Returns when there's a match for $expect, which can take the form of a string literal or, if $mode == self::READ_REGEX, a regular expression.
See also: self::write()
public read ( string $expect = '', integer $mode = self::READ_SIMPLE ) : string
$expect string
$mode integer
return string
    function read($expect = '', $mode = self::READ_SIMPLE)
    {
        $this->curTimeout = $this->timeout;
        $this->is_timeout = false;
        if (!($this->bitmap & self::MASK_LOGIN)) {
            throw new \RuntimeException('Operation disallowed prior to login()');
        }
        if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) {
            throw new \RuntimeException('Unable to initiate an interactive shell session');
        }
        $channel = $this->_get_interactive_channel();
        $match = $expect;
        while (true) {
            if ($mode == self::READ_REGEX) {
                preg_match($expect, substr($this->interactiveBuffer, -1024), $matches);
                $match = isset($matches[0]) ? $matches[0] : '';
            }
            $pos = strlen($match) ? strpos($this->interactiveBuffer, $match) : false;
            if ($pos !== false) {
                return Strings::shift($this->interactiveBuffer, $pos + strlen($match));
            }
            $response = $this->_get_channel_packet($channel);
            if (is_bool($response)) {
                $this->in_request_pty_exec = false;
                return $response ? Strings::shift($this->interactiveBuffer, strlen($this->interactiveBuffer)) : false;
            }
            $this->interactiveBuffer .= $response;
        }
    }

Usage Example

Example #1
1
 /**
  * Allow to execute and get the result of a command which can return more than one page on the CLI
  *
  * @param string   $cmd      The command to run
  * @param string   $regexp   A regex which will filter the rows. If a rows doesn't satisfy the regex, it will be
  *                           skipped.
  * @param Callable $callback A callback wich will be called for each rows. The first parameter is the rows itself,
  *                           the second parameter is the result of preg_match call on the row wich the regexp
  *                           provided
  * 
  * @return array
  *
  * @throws InvalidArgumentException If $callback is not a valid callback
  */
 protected function execPageableCommand(string $cmd, string $regexp, $callback) : array
 {
     if (false == is_callable($callback)) {
         throw new InvalidArgumentException("You must provide a valid callback");
     }
     $result = [];
     $this->ssh->write($cmd . $this->enterKey);
     $readPattern = '`' . $this->morePattern . '|' . $this->promptPattern . '`';
     while ($stdout = $this->ssh->read($readPattern, SSH2::READ_REGEX)) {
         foreach (explode("\n", $stdout) as $line) {
             if ($regexp == '' || $regexp == '`.*`' || $regexp == '`.+`') {
                 $result[] = $line;
                 continue;
             }
             preg_match($regexp, $line, $match);
             if (count($match) > 0) {
                 list($index, $value) = $callback($line, $match);
                 if ($index !== null) {
                     $result[$index] = $value;
                 } else {
                     $result[] = $value;
                 }
             }
         }
         if (preg_match('`' . $this->promptPattern . '`', $stdout) === 1) {
             break;
         }
         $this->ssh->write($this->spaceKey);
     }
     return $result;
 }
All Usage Examples Of phpseclib\Net\SSH2::read