summaryrefslogtreecommitdiff
path: root/auth/ssh.php
diff options
context:
space:
mode:
Diffstat (limited to 'auth/ssh.php')
-rw-r--r--auth/ssh.php40
1 files changed, 13 insertions, 27 deletions
diff --git a/auth/ssh.php b/auth/ssh.php
index 2b67541..68fd927 100644
--- a/auth/ssh.php
+++ b/auth/ssh.php
@@ -19,6 +19,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+require_once 'Crypt/RSA.php';
+require_once 'Net/SSH2.php';
require_once 'authentication.php';
final class SSH extends Authentication {
@@ -38,33 +40,27 @@ final class SSH extends Authentication {
}
if ($this->config['auth'] == 'ssh-key') {
- if (!isset($this->config['user']) || !isset($this->config['public_key'])
- || !isset($this->config['private_key'])) {
- throw new Exception('User and key pair required for ssh-key.');
+ if (!isset($this->config['user']) || !isset($this->config['private_key'])) {
+ throw new Exception('User and private key required for ssh-key.');
}
}
}
public function connect() {
- $this->connection = ssh2_connect($this->config['host'], $this->port);
- if (!$this->connection) {
- throw new Exception('Cannot connect to router.');
- }
-
+ $this->connection = new Net_SSH2($this->config['host'], $this->port);
$success = false;
if ($this->config['auth'] == 'ssh-password') {
- $success = ssh2_auth_password($this->connection, $this->config['user'],
- $this->config['pass']);
+ $success = $this->connection->login($this->config['user'], $this->config['pass']);
} else if ($this->config['auth'] == 'ssh-key') {
+ $key = new Crypt_RSA();
+ $key->loadKey(file_get_contents($this->config['private_key']));
+
if (isset($this->config['pass'])) {
- $success = ssh2_auth_pubkey_file($this->connection, $this->config['user'],
- $this->config['public_key'], $this->config['private_key'],
- $this->config['pass']);
- } else {
- $success = ssh2_auth_pubkey_file($this->connection, $this->config['user'],
- $this->config['public_key'], $this->config['private_key']);
+ $key->setPassword($this->config['pass']);
}
+
+ $success = $this->connection->login($this->config['user'], $key);
} else {
throw new Exception('Unknown type of connection for SSH.');
}
@@ -79,17 +75,7 @@ final class SSH extends Authentication {
$this->connect();
}
- if (!($stream = ssh2_exec($this->connection, $command))) {
- throw new Exception('SSH command failed');
- }
-
- stream_set_blocking($stream, true);
-
- $data = '';
- while ($buf = fread($stream, 4096)) {
- $data .= $buf;
- }
- fclose($stream);
+ $data = $this->connection->exec($command);
return $data;
}