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
示例#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;
 }
示例#2
0
 /**
  * Send authentication
  *
  * @return bool
  */
 protected function _auth()
 {
     // check to see if already authenticated
     if ($this->_authenticated === true) {
         return true;
     }
     switch ($this->config('ssh.authType')) {
         case 'password':
             // perform password auth
             if ($this->_phpseclib->login($this->_credentials->getUsername(), $this->_credentials->getPassword())) {
                 //get last line and set as prompt
                 $response = $this->_phpseclib->read($this->config('prompt.command'));
                 $lines = split("\n", $response);
                 $this->config('prompt.command', trim(end($lines)));
                 $this->_authenticated = true;
                 return true;
             }
             break;
         case 'publicKey':
             // perform public key auth
             $this->_writeTmpKeys();
             $privkey = new \Crypt_RSA();
             //phpseclib has no DSA support :(
             $privkey->loadKey($this->_pubKeyTempFile);
             if ($this->_phpseclib->login($this->_credentials->getUsername(), $privkey)) {
                 $this->_destroyTmpKeys();
                 $this->_authenticated = true;
                 return true;
             }
             $this->_destroyTmpKeys();
             break;
     }
     //end switch()
     //return false;
     throw new \Exception('SSH ' . $this->config('ssh.authType') . ' Authentication Failed');
 }
示例#3
0
文件: ssh.php 项目: codebubb/web_ssh
<?php

include 'vendor/autoload.php';
use phpseclib\Net\SSH2;
use phpseclib\File\ANSI;
$ansi = new ANSI();
echo <<<HTML
\t<form action="ssh.php" method="POST">
\t\t<label for="host">Host</label>
\t\t<input name="host">
\t\t<label for="user">Username</label>
\t\t<input name="user">
\t\t<label for="pass">Password</label>
\t\t<input name="pass" type="password">
\t\t<label for="port">Port</label>
\t\t<input name="port" value="22">
\t\t<input type="submit">
\t</form>
HTML;
extract($_POST);
if (isset($host, $user, $pass)) {
    $ssh = new SSH2($host, $port);
    if (!$ssh->login($user, $pass)) {
        exit('Login Failed');
    }
    $ansi->appendString($ssh->read('username@username:~$'));
    $ansi->appendString($ssh->read());
    echo $ansi->getScreen();
}