/** * Execute the specified command. * * @param string $std_output The standard output of the executed command * @param string $std_error The standard error of the executed command * * @throws Net_SSH2_Exception If the system does not support PTY. * * @return mixed The exit code of the executed command or false on error */ protected function exec($command, &$std_output, &$std_error) { $exit_code = false; //This value can be set in the createCommandLine method implementation $std_input = $this->_std_input; $descriptorspec = array(0 => array("pty"), 1 => array("pty"), 2 => array("pty")); try { $process = proc_open($command, $descriptorspec, $pipes); } catch (Exception $e) { if ($this->password !== null) { //Only public/private key authentication is supported. throw new Net_SSH2_Exception(Net_SSH2::getMessage(SSH2_PTY_NOT_SUPPORTED, $e->getMessage())); } exec($command, $std_output, $exit_code); return $exit_code; } if (is_resource($process)) { stream_set_blocking($pipes[0], false); stream_set_blocking($pipes[1], false); stream_set_blocking($pipes[2], false); if ($std_input !== null) { $std_output .= fgets($pipes[1], 4096); sleep(2); fwrite($pipes[0], $std_input . "\n"); fflush($pipes[0]); } while (!feof($pipes[1])) { $std_output .= fgets($pipes[1], 4096); } while (!feof($pipes[2])) { $std_error .= fgets($pipes[2], 4096); } fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); $exit_code = proc_close($process); } return $exit_code; }
/** * Install your public key in a remote machine’s authorized_keys * * @param string $std_output The standard output of the executed command * @param string $std_error The standard error of the executed command * @param array $options Additional options for the specified method * * @throws Net_SSH2_Exception If the public key is not found. * * @return mixed The exit code of the executed command or false on error */ public function sshCopyId(&$std_output, &$std_error, $options = array()) { //Check for valid options foreach ($options as $key => $value) { $this->{$key} = $value; } if (!is_readable($this->public_identity_file)) { throw new Net_SSH2_Exception(Net_SSH2::getMessage(SSH2_PUBLIC_KEY_UNAVAILABLE)); } $exit_code = 255; $pub_key = trim(File::readAll($this->public_identity_file), "\n"); $pub_key_array = explode(' ', $pub_key); try { $connection = $this->_authenticate($std_output); $pkey = ssh2_publickey_init($connection); ssh2_publickey_add($pkey, $pub_key_array[0], base64_decode($pub_key_array[1]), false, array('comment' => $pub_key_array[2])); $exit_code = 0; } catch (Exception $e) { $std_output = $e->getMessage(); } return $exit_code; }
/** * Overloading of the __set method * * @param string $key The name of the properties that should be set * @param mixed $value parameter specifies the value that the object * should set the $key * * @throws Net_SSH2_Exception If trying to set an undefined properties. * @return mixed True on success */ public function __set($key, $value) { if (!key_exists($key, $this->allowed_options)) { throw new Net_SSH2_Exception(Net_SSH2::getMessage(SSH2_OPTION_NOT_VALID, $key)); } $this->options[$key] = $value; return true; }