Skip to content

Instantly share code, notes, and snippets.

@skydiablo
Created August 3, 2011 12:04
Show Gist options
  • Save skydiablo/1122473 to your computer and use it in GitHub Desktop.
Save skydiablo/1122473 to your computer and use it in GitHub Desktop.
PHP SSH2 Wrapper
<?php
class ssh2 {
const SSH_COMMAND_FINISHED_FLAG = '___SSH_COMMAND_FINISHED___';
private $host = 'localhost';
private $port = 22;
private $username = 'root';
private $password = '';
private $connectionMethods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => '3des-cbc',
'comp' => 'none'),
'server_to_client' => array(
'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
'comp' => 'none'));
private $publicKeyFile;
private $privateKeyFile;
private $keyPassphrase;
private $connection = null;
/**
* @param string $host
* @param int $port
*/
public function __construct($host = 'localhost', $port = 22) {
if (!function_exists("ssh2_connect")) {
throw new Exception('function ssh2_connect doesn\'t exist');
}
$this->setHost($host);
$this->setPort($port);
}
/**
* @param array $connectionMethods
* @return bool
*/
public function connect($connectionMethods = false) {
$this->setConnectionMethods(is_array($connectionMethods) ? $connectionMethods : $this->getConnectionMethods());
$this->connection = ssh2_connect($this->getHost(), $this->getPort(), $this->getConnectionMethods());
return (bool) $this->connection;
}
public function disconnect() {
return $this->exec('exit;', 10, false);
}
/**
* @return string
*/
public function getFingerprint() {
if ($this->connection) {
return ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
}
return false;
}
/**
* @param string $username
* @return array
*/
public function getAuthMethods($username) {
return (array) ssh2_auth_none($this->connection, $username);
}
/**
* @param string $username
* @param string $password
* @return bool
*/
public function authPassword($username, $password) {
if ($this->connection) {
$this->setUsername($username);
$this->setPassword($password);
return (bool) ssh2_auth_password($this->connection, $this->getUsername(), $this->getPassword());
}
return false;
}
/**
* @param string $username
* @param string $publicKeyFile
* @param string $privateKeyFile
* @param string $keyPassphrase
* @return bool
*/
public function authKey($username, $publicKeyFile, $privateKeyFile, $keyPassphrase = null) {
if ($this->connection && is_file($publicKeyFile) && is_readable($publicKeyFile) && is_file($privateKeyFile) && is_readable($privateKeyFile)) {
$this->setPublicKeyFile($publicKeyFile);
$this->setPrivateKeyFile($privateKeyFile);
$this->setKeyPassphrase($keyPassphrase);
return (bool) ssh2_auth_pubkey_file($this->connection, $this->ssh_auth_user, $this->ssh_auth_pub, $this->ssh_auth_priv, $this->ssh_auth_pass);
}
}
/**
* @param string $cmd
* @param int $timeout
* @return string
*/
public function exec($cmd, $timeout = 10, $saveMode = true) {
if ($saveMode) {
$cmd .= ' ; echo "' . self::SSH_COMMAND_FINISHED_FLAG . '"';
}
if (!($stream = ssh2_exec($this->connection, $cmd))) {
throw new Exception('SSH command failed');
}
stream_set_blocking($stream, true);
$data = "";
$time_start = time();
$data = "";
if ($saveMode) {
while (true) {
$data .= fread($stream, 4096);
$flagPosition = strpos($data, self::SSH_COMMAND_FINISHED_FLAG);
if ($flagPosition !== false) {
if (substr_compare($data, self::SSH_COMMAND_FINISHED_FLAG, $flagPosition, strlen(self::SSH_COMMAND_FINISHED_FLAG)) == 0) {
$data = substr($data, 0, (strlen(self::SSH_COMMAND_FINISHED_FLAG) + 1) * -1);
}
break;
}
if ((time() - $time_start) > (int) $timeout) {
fclose($stream);
throw new Exception('ssh execution timeout');
}
}
} else {
while ($buf = fread($stream, 4096)) {
$data .= $buf;
}
}
fclose($stream);
return $data;
}
/**
* @param string $userPassword
* @param string $cmd
* @param int $timeout
* @param boolean $saveMode
* @return string
*/
public function sudoExec($userPassword, $cmd, $timeout = 10, $saveMode = true) {
$ssh2->exec('sudo -k', $timeout, $saveMode);
return $ssh2->exec('echo "' . $userPassword . '" | sudo ' . $cmd, $timeout, $saveMode);
}
public function getHost() {
return $this->host;
}
public function setHost($host) {
$this->host = $host;
}
public function getPort() {
return $this->port;
}
public function setPort($port) {
$this->port = (int) $port;
}
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
}
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
}
public function getPublicKeyFile() {
return $this->publicKeyFile;
}
public function setPublicKeyFile($publicKey) {
$this->publicKeyFile = $publicKey;
}
public function getPrivateKeyFile() {
return $this->privateKeyFile;
}
public function setPrivateKeyFile($privateKey) {
$this->privateKeyFile = $privateKey;
}
public function getKeyPassphrase() {
return $this->keyPassphrase;
}
public function setKeyPassphrase($keyPassphrase) {
$this->keyPassphrase = $keyPassphrase;
}
public function getConnectionMethods() {
return $this->connectionMethods;
}
public function setConnectionMethods($connectionMethods) {
$this->connectionMethods = $connectionMethods;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment