-
Notifications
You must be signed in to change notification settings - Fork 2
Some changes to allow commection with rsa keys authentication #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /.idea |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace steevanb\SSH2Bundle\Factory; | ||
|
|
||
| use steevanb\SSH2Bundle\Model\Profile; | ||
| use steevanb\SSH2Bundle\Model\Connection; | ||
|
|
||
| /** | ||
| * Service for SSH2 connections | ||
| */ | ||
| class ConnectionFactory | ||
| { | ||
| /** | ||
| * @param array $args | ||
| * @param bool $autoConnect | ||
| * @return Connection | ||
| */ | ||
| public function connect(array $args, $autoConnect = true) | ||
| { | ||
| $profile = new Profile($args); | ||
| return new Connection($profile, $autoConnect); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| <?php | ||
|
|
||
| namespace steevanb\SSH2Bundle\Entity; | ||
| namespace steevanb\SSH2Bundle\Model; | ||
|
|
||
| use steevanb\SSH2Bundle\Exception\ConnectionException; | ||
|
|
||
|
|
@@ -55,7 +55,10 @@ public function connect() | |
| if ($this->connection === false) { | ||
| $this->state = self::STATE_INVALID_ADDRESS; | ||
| } else { | ||
| $auth = @ssh2_auth_password($this->connection, $profile->getLogin(), $profile->getPassword()); | ||
| $auth = @ssh2_auth_pubkey_file($this->connection, $profile->getLogin(), $profile->getRsaPubKey(), $profile->getRsaPemKey(), $profile->getPassphrase()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe do it only when a RsaPubKey is specified, instead of try to connect with an empty Rsa, then connect without Rsa ? |
||
| if ($auth === false) { | ||
| $auth = @ssh2_auth_password($this->connection, $profile->getLogin(), $profile->getPassword()); | ||
| } | ||
| if ($auth === false) { | ||
| $this->state = self::STATE_INVALID_LOGIN; | ||
| } else { | ||
|
|
@@ -119,20 +122,40 @@ public function getProfile() | |
| return $this->profile; | ||
| } | ||
|
|
||
| public function getConnection() | ||
| { | ||
| return $this->connection; | ||
| } | ||
|
|
||
| /** | ||
| * Execute a command | ||
| * | ||
| * @param string $command | ||
| * @return string | ||
| * @param $command | ||
| * @return bool|string | ||
| * @throws ConnectionException | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't add this PHPDoc, always wrong :) |
||
| * | ||
| */ | ||
| public function exec($command) | ||
| { | ||
| $this->assertConnected(); | ||
|
|
||
| $stream = ssh2_exec($this->connection, $command); | ||
| stream_set_blocking($stream, true); | ||
| $streamOut = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); | ||
| return stream_get_contents($streamOut); | ||
| if( !($stream = ssh2_exec($this->connection, $command)) ) { | ||
| throw new ConnectionException( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok to throw an exception if command fail, but not a ConnectionException. Maybe use deleted Exception, or create CommandException |
||
| sprintf('%s command failed through ssh', $command), | ||
| $this->profile | ||
| ); | ||
| } | ||
| $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); | ||
| stream_set_blocking($errorStream, TRUE); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| stream_set_blocking($stream, TRUE); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $output = stream_get_contents($stream); | ||
| $error_output = stream_get_contents($errorStream); | ||
| fclose($stream); | ||
| fclose($errorStream); | ||
| if (!empty($error_output)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i prefer |
||
| throw new ConnectionException($error_output, $this->profile); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok to throw an exception if command fail, but not a ConnectionException. Maybe use deleted Exception, or create CommandException |
||
| } | ||
| return $output; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -141,6 +164,7 @@ public function exec($command) | |
| * @param string $command | ||
| * @param string $separator | ||
| * @return array | ||
| * @throws ConnectionException | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't add this PHPDoc |
||
| */ | ||
| public function execExplode($command, $separator = ' ') | ||
| { | ||
|
|
@@ -155,6 +179,7 @@ public function execExplode($command, $separator = ' ') | |
| * @param string $separator Separator | ||
| * @param mixed $default Default value if $index is not found | ||
| * @return string | ||
| * @throws ConnectionException | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't add this PHPDoc |
||
| */ | ||
| public function execExplodeIndex($command, $index, $separator = ' ', $default = null) | ||
| { | ||
|
|
@@ -167,6 +192,7 @@ public function execExplodeIndex($command, $index, $separator = ' ', $default = | |
| * | ||
| * @param type $command | ||
| * @return array | ||
| * @throws ConnectionException | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't add this PHPDoc |
||
| */ | ||
| public function execLines($command) | ||
| { | ||
|
|
@@ -182,6 +208,7 @@ public function execLines($command) | |
| * @param int $index Index of the line to return | ||
| * @param string $default Default value | ||
| * @return string | ||
| * @throws ConnectionException | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't add this PHPDoc |
||
| */ | ||
| public function execLine($command, $index = 0, $default = null) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pattern Factory is a good one, but when there is just 2 lines of code, not sure it's usefull to create a service just for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You did the same, I just move the Service\Connection.php class to Factory\ConnectionFactory.php