/** * 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; }
/** * 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'); }
<?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(); }