/** * */ public function connect() { $this->ssh = ssh2_connect($this->configuration['hostname'], $this->configuration['port']); $username = $this->configuration['username']; switch ($this->configuration[SftpDriver::CONFIG_AUTHENTICATION_METHOD]) { case static::AUTHENTICATION_PASSWORD: ssh2_auth_password($this->ssh, $username, $this->configuration['password']); break; case static::AUTHENTICATION_PUBKEY: $publicKey = $this->configuration['publicKey']; $privateKey = $this->configuration['privateKey']; if (!file_exists($publicKey) || !file_exists($privateKey)) { return; } $password = $this->configuration['privateKeyPassword']; if (empty($password)) { $password = null; } ssh2_auth_pubkey_file($this->ssh, $username, $publicKey, $privateKey, $password); break; default: } $this->sftp = ssh2_sftp($this->ssh); $this->sftpWrapper = 'ssh2.sftp://' . $this->sftp; $this->sftpWrapperLength = strlen($this->sftpWrapper); $this->iteratorFlags = \FilesystemIterator::UNIX_PATHS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::FOLLOW_SYMLINKS; return true; }
/** * Connect operation */ public function connect() { if ($this->_ssh2 != null) { // Already connected return; } // Connect to server $host = isset($this->_config['hostname']) ? $this->_config['hostname'] : 'localhost'; $port = isset($this->_config['port']) ? $this->_config['port'] : 22; $username = isset($this->_config['username']) ? $this->_config['username'] : ''; $password = isset($this->_config['password']) ? $this->_config['password'] : null; $this->_ssh2 = ssh2_connect($host, $port); if ($this->_ssh2 === FALSE) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_open_connection'), array(':host' => $host, 'port' => $port)); } // Check fingerprint if it is specified if (isset($this->_config['fingerprint'])) { if (strtolower(ssh2_fingerprint($this->_ssh2)) != strtolower($this->_config['fingerprint'])) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_fingerprint_validation'), array(':key' => ssh2_fingerprint($this->_ssh2))); } } // Connect with certificate if it is specified if (isset($this->_config['pubkeyfile']) and isset($this->_config['privkeyfile'])) { if (!@ssh2_auth_pubkey_file($this->_ssh2, $username, $this->_config['pubkeyfile'], $this->_config['privkeyfile'], $password)) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_authentication')); } } else { if (!@ssh2_auth_password($this->_ssh2, $username, $password)) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_authentication')); } } // Enable SFTP mode $this->_sftp = ssh2_sftp($this->_ssh2); }
public function __construct($config) { foreach ($config as $name => $value) { $this->{$name} = $value; } $methods = array(); if (!isset($this->auth['method'])) { error('Unspecified authentication method.'); } // Connect $this->connection = ssh2_connect($this->host, isset($this->port) ? $this->port : 22, $methods); switch ($this->auth['method']) { case 'pubkey': if (!isset($this->auth['public'])) { error('Public key filename not specified.'); } if (!isset($this->auth['private'])) { error('Private key filename not specified.'); } if (!ssh2_auth_pubkey_file($this->connection, $this->auth['username'], $this->auth['public'], $this->auth['private'], isset($this->auth['passphrase']) ? $this->auth['passphrase'] : null)) { error('Public key authentication failed.'); } break; case 'plain': if (!ssh2_auth_password($this->connection, $this->auth['username'], $this->auth['password'])) { error('Plain-text authentication failed.'); } break; default: error('Unknown authentication method: "' . $this->auth['method'] . '".'); } }
function __construct($cfg) { // set default options if missing static $defaults = array('port' => 22, 'user' => 'root'); if (is_object($cfg)) { $cfg = json_decode(json_encode($cfg), 1); } foreach ($defaults as $k => $v) { if (!isset($cfg[$k])) { $cfg[$k] = $v; } } // connect ssh2 $this->ssh2 = ssh2_connect($cfg['host'], $cfg['port']); if (!$this->ssh2) { throw new \Exception("can't connect trough ssh2\n"); } // authorize if (isset($cfg['key'])) { // private/public key authentication requested if (!ssh2_auth_pubkey_file($this->ssh2, $cfg['user'], $cfg['key']['pub'], $cfg['key']['pvt'], isset($cfg['key']['pass']) ? $cfg['key']['pass'] : NULL)) { throw new \Exception("can't authorize via key"); } } elseif (isset($cfg['pass'])) { // username & password authentication if (!ssh2_auth_password($this->ssh2, $cfg['user'], $cfg['pass'])) { throw new \Exception("can't authorize via user & pass"); } } else { throw new \Exception("not enough authentication information provided"); } $this->sftp = ssh2_sftp($this->ssh2); }
public function connect() { $methods = array(); if ($this->use_pubkey_file_) { $methods['hostkey'] = 'ssh-rsa'; } $conn = ssh2_connect($this->conf_['host'], $this->conf_['port'], $methods); if (false === $conn) { $this->setErr_(-1, sprintf('failed to connect [%s:%d]', $this->conf_['host'], $this->conf_['port'])); return false; } if ($this->use_pubkey_file_) { $rc = ssh2_auth_pubkey_file($conn, $this->conf_['user'], $this->conf_['pubkey_file'], $this->conf_['privkey_file'], isset($this->conf_['passphrase']) ? $this->conf_['passphrase'] : NULL); if (false === $rc) { $this->setErr_(-1, sprintf('failed to auth with[%s:%s,%s,%s]', $this->conf_['user'], $this->conf_['pubkey_file'], $this->conf_['privkey_file'], $this->conf_['passphrase'])); return false; } } else { $rc = ssh2_auth_password($conn, $this->conf_['user'], $this->conf_['passwd']); if (false === $rc) { $this->setErr_(-1, sprintf('failed to auth with[%s:%s]', $this->conf_['user'], $this->conf_['passwd'])); return false; } } $this->conn_ = $conn; return true; }
function getOS($pass = false) { $ip = $_REQUEST['ip']; $sshp = $_REQUEST['sshp']; if (!isset($_REQUEST['sshp'])) { $sshp = '22'; } $lsbresult1 = array(); $methods = array('hostkey', 'ssh-rsa'); if (isset($pass) && $pass) { $methods = array(); } $connection = ssh2_connect($ip, $sshp, $methods); if (!$connection) { throw new Exception("fail: unable to establish connection, please Check IP or if server is on and connected"); } $pass_success = false; if ($methods) { $rsa_pub = realpath($_SERVER['HOME'] . '/.ssh/id_rsa.pub'); $rsa = realpath($_SERVER['HOME'] . '/.ssh/id_rsa'); $pass_success = ssh2_auth_pubkey_file($connection, 'sysad', $rsa_pub, $rsa); } else { $pass_success = ssh2_auth_password($connection, 'root', $pass); } if (!$pass_success) { throw new Exception("fail: unable to establish connection\nPlease Check your password"); } $cmd = "lsb_release -as"; $stream = ssh2_exec($connection, $cmd); $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); stream_set_blocking($errorStream, true); stream_set_blocking($stream, true); $lsbresult1 = stream_get_contents($stream); stream_set_blocking($errorStream, false); stream_set_blocking($stream, false); flush(); fclose($errorStream); fclose($stream); fclose($rsa_pub); fclose($rsa); unset($connection); print_r($lsbresult1); $lsbresult = explode("\n", $lsbresult1); if (!empty($lsbresult)) { $OS = $lsbresult[0]; $version = $lsbresult[3]; $releasever = $lsbresult[2]; } else { echo "No values present"; die; } ssh2_exec($connection, 'exit'); fclose($stream); fclose($errorStream); flush(); unset($connection); fclose($connection); return array($OS, $version, $releasever); }
public function __construct(FtpConfig $config) { $this->_config = $config; $this->_session = ssh2_connect($this->_config->getHost(), $this->_config->getPort()); if (!ssh2_auth_pubkey_file($this->_session, $this->_config->getUser(), $this->_config->getPublicKey(), $this->_config->getPrivateKey())) { throw new \Exception("Could not connect to Ftp server with provided configuration"); } }
public function authPubkey($user, $pubkeyFile, $privkeyFile, $passphrase) { if (!ssh2_auth_pubkey_file($this->connection, $user, $pubkeyFile, $privkeyFile, $passphrase)) { $this->logs[] = "Public Key Authorization failed"; return false; } return true; }
public function connect() { $this->connection = ssh2_connect($this->settings['host'], $this->settings['port']); if (!$this->connection) { throw new \Exception("Could not connect to remote server."); } ssh2_auth_pubkey_file($this->connection, $this->settings['user'], $this->settings['publicKey'], $this->settings['privateKey']); }
public function authenticate($user, $public_key, $private_key) { if (!$this->authenticated) { if (false === ($this->authenticated = ssh2_auth_pubkey_file($this->connection, $user, $public_key, $private_key))) { throw new SshException(sprintf('Authorization failed for user "%s"', $user)); } } }
function ssh_connect($address) { include 'config.php'; $tmp = explode(":", $address); $ip = $tmp[0]; $port = $tmp[1]; global $connection; $connection = ssh2_connect($ip, $port, array('hostkey' => 'ssh-rsa')); ssh2_auth_pubkey_file($connection, $ssh_user, $ssh_key_path . 'id_rsa.pub', $ssh_key_path . 'id_rsa'); }
protected function doLoginPubKey($user, $pubKeyFile, $privKeyFile, $passphrase = null) { // try to login if (ssh2_auth_pubkey_file($this->getSsh2Connection(), $user, $pubKeyFile, $privKeyFile, $passphrase)) { $this->sftp_id = ssh2_sftp($this->getSsh2Connection()); return $this->sftp_id != false && $this->sftp_id != null; } else { return false; } }
/** * * @throws \Exception * @return boolean */ public function connect() { $this->session = ssh2_connect($this->config->host, $this->config->port); if (false === $this->session) { throw new \Exception("Echec de la connexion ssh " . print_r($this->config->toArray(), true)); } $auth = ssh2_auth_pubkey_file($this->session, $this->config->username, $this->config->pubkeyfile, $this->config->privkeyfile); if (true !== $auth) { throw new \Exception("Echec de l'authentification ssh " . print_r($this->config->toArray(), true)); } return true; }
/** * Connect to server */ protected function connect() { $this->session = ssh2_connect($this->hostname); switch ($this->authType) { case self::AUTH_PASSWORD: ssh2_auth_password($this->session, $this->user, $this->password); break; case self::AUTH_KEY: ssh2_auth_pubkey_file($this->session, $this->user, $this->pubKey, $this->privKey, $this->password); break; } }
public function connect() { $this->connection = @ssh2_connect($this->hostname, $this->port, array('hostkey' => 'ssh-rsa')); $fingerprint = ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_SHA1 | SSH2_FINGERPRINT_HEX); if (!@ssh2_auth_pubkey_file($this->connection, $this->username, $this->pubKeyFile, $this->privKeyFile)) { throw new Exception("Authentification Failed"); } $this->sftpSession = @ssh2_sftp($this->connection); if (!$this->sftpSession) { throw new Exception("Could not initialize SFTP subsystem."); } }
/** * @param resource $resource * @throws \Exception */ public function authenticate($resource) { if (!file_exists($this->publicKeyFile) || !is_readable($this->publicKeyFile)) { throw new \Exception("Public key file stored in '{$this->publicKeyFile}' was not found or is not readable"); } if (!file_exists($this->privateKeyFile) || !is_readable($this->privateKeyFile)) { throw new \Exception("Private key file stored in '{$this->privateKeyFile}' was not found or is not readable"); } if (!@ssh2_auth_pubkey_file($resource, $this->username, $this->publicKeyFile, $this->privateKeyFile, $this->passPhrase)) { // @ prevent warning, on invalid authentication throws exception throw new \Exception("Authentication failed for user '{$this->username}' using public key: Username/PublicKey combination invalid"); } }
public function connect() { $this->connection = ssh2_connect($this->host, $this->port); if ($this->username && $this->publicKey && $this->privateKey) { ssh2_auth_pubkey_file($this->connection, $this->username, $this->publicKey, $this->privateKey, $this->passPhrase); } else { if ($this->username && $this->password) { ssh2_auth_password($this->connection, $this->username, $this->password); } else { throw new \Exception('No authentication method specified - use setUsername and setPassword for password based authentication or setUsername and setKeys for key based authentication'); } } }
/** * @param $resource * @throws \OOSSH\Exception\AuthenticationFailed */ public function authenticate($resource) { $pubkeyFile = $this->pubkeyFile; if (!is_file($pubkeyFile)) { $pubkeyFile = KeyFinder::find(KeyFinder::TYPE_PUBLICKEY); } $privkeyFile = $this->privkeyFile; if (!is_file($privkeyFile)) { $privkeyFile = KeyFinder::find(KeyFinder::TYPE_PRIVATEKEY); } if (!\ssh2_auth_pubkey_file($resource, $this->username, $pubkeyFile, $privkeyFile, $this->passphrase)) { throw new Exception\AuthenticationFailed(); } }
private function connect($host, $target, $pwd = NULL) { $this->name = $host; $this->log->verbose("Connecting to {$host} on port {$target['ssh_port']}"); if (!($conn = ssh2_connect($host, $target['ssh_port'], NULL, array('disconnect', array($this, 'disconnect'))))) { $this->log->error("Unable to connect"); } $this->log->verbose("Authenticating as {$target['deploy_user']} using {$target['public_key_file']}"); if (!ssh2_auth_pubkey_file($conn, $target['deploy_user'], $target['public_key_file'], $target['private_key_file'], $pwd)) { $this->log->error("Unable to authenticate"); } $this->log->ssh[$host] = $this; return $conn; }
public function connect() { try { $this->socket = ssh2_connect($this->server, $this->port, $this->methods, $this->callbacks); } catch (Exception $e) { $this->error_handler($e); } try { ssh2_auth_pubkey_file($this->socket, $this->username, $this->pub_key, $this->priv_key); return true; } catch (Exception $e) { $this->error_handler($e); return false; } }
/** * Connects to FTP server. * @return void */ public function connect() { $this->protect(function () { $parts = parse_url($this->url); $this->connection = ssh2_connect($parts['host'], empty($parts['port']) ? 22 : (int) $parts['port']); if (isset($parts['pass'])) { ssh2_auth_password($this->connection, $parts['user'], $parts['pass']); } elseif ($this->publicKey != '') { // intentionally != ssh2_auth_pubkey_file($this->connection, $parts['user'], $this->publicKey, $this->privateKey); } else { ssh2_auth_agent($this->connection, urldecode($parts['user'])); } $this->sftp = ssh2_sftp($this->connection); }); }
/** * @param $server * @param $port * @param array $options * @throws \Exception */ function __construct($server, $port, $options = array()) { $options = array_merge(array('auth' => '', 'user' => '', 'password' => '', 'ssh-key' => ''), $options); $this->session = ssh2_connect($server, $port); if ($this->session) { switch ($options['auth']) { case self::AUTH_PASSWORD: ssh2_auth_password($this->session, $options['user'], $options['password']); break; case self::AUTH_SSH_KEY: ssh2_auth_pubkey_file($this->session, $options['user'], $options['ssh-key'] . '.pub', $options['ssh-key']); break; default: throw new \Exception(sprintf('"%s" not recognized as an auth mode for ssh2', $options['auth'])); } } }
public function connect($host, $login, $password) { if (!parent::connect($host, $login, $password)) { return false; } if (empty($this->port)) { $this->port = 22; } $this->handle = empty($this->key) ? @ssh2_connect($this->host, $this->port) : @ssh2_connect($this->host, $this->port, $this->hostkey); if ($this->handle) { $authresult = $this->public_key && $this->private_key ? @ssh2_auth_pubkey_file($this->handle, $this->login, $this->public_key, $this->private_key, $this->password) : @ssh2_auth_password($this->handle, $this->login, $this->password); if ($authresult) { $this->sftp = ssh2_sftp($this->handle); $this->connected = true; return true; } } return false; }
public function connect() { if (empty($this->connection)) { unset($this->resource); $this->ssh2_connect(); } if (empty($this->resource)) { if (!empty($this->pubkeyfile) && !empty($this->privkeyfile)) { if (!ssh2_auth_pubkey_file($this->connection, $this->login, $this->pubkeyfile, $this->privkeyfile, $this->password)) { throw new \RuntimeException('Could not authenticated SftpProtocol connection with public key'); } } elseif (!ssh2_auth_password($this->connection, $this->login, $this->password)) { throw new \RuntimeException('Could not authenticated SftpProtocol connection with login and password'); } } if (!($this->resource = ssh2_sftp($this->connection))) { throw new \RuntimeException('Could not proccessiong SftpProtocol connection'); } return $this; }
public function boot($classes) { $res = $this->client->request('POST', self::API_URL . '/servers', ['http_errors' => false, 'json' => ['organization' => $this->organization, 'name' => uniqid() . '-' . getmypid(), 'image' => $this->image, 'tags' => ['gearman']]]); $raw = json_decode($res->getBody(), true); $server = $raw['server']; $res = $this->client->request('POST', self::API_URL . '/servers/' . $server['id'] . '/action', ['http_errors' => false, 'json' => ['action' => 'poweron']]); $raw = json_decode($res->getBody(), true); $host = new Ping($server['public_ip']); do { $latency = $host->ping(); sleep(1); } while (!$latency); $consoleAsync = new ConsoleAsync($this->console); $bootstrap = tempnam(); file_put_contents($bootstrap, json_encode($this->getBootstrap())); $connection = ssh2_connect($server['public_ip'], 22); ssh2_auth_pubkey_file($connection, self::DEFAULT_USER, $this->keys['public'], $this->keys['private']); ssh2_spc_send($connection, $tmp, $this->bootstrap); ssh2_exec($session, $consoleAsync->getCommandString(new HireWorkerCommand(), ['classes' => $classes], ['type' => Worker::LOCAL])); }
function run_script_on_server($script, $server, $user, $public_key_file, $private_key_file) { $con = ssh2_connect($server); if ($con === false) { return array(false, false); } $result = ssh2_auth_pubkey_file($con, $user, $public_key_file, $private_key_file); if (!$result) { return array(false, false); } $remote_script = basename($script); $result = ssh2_scp_send($con, $script, $remote_script, 0700); if (!$result) { return array(false, false); } $io_stream = ssh2_exec($con, "( ./{$remote_script} ) 2>&1; echo \$?"); if ($io_stream) { stream_set_blocking($io_stream, true); $output = ''; while (true) { $line = fgets($io_stream); if ($line === false) { break; } $output .= $line; } fclose($io_stream); $output = rtrim($output); $last_newline_index = strrpos($output, "\n"); $status_code = intval(substr($output, $last_newline_index + 1)); $output = substr($output, 0, $last_newline_index); } else { $output = false; $status_code = false; } $sftp_con = ssh2_sftp($con); if ($sftp_con) { ssh2_sftp_unlink($sftp_con, $remote_script); } return array($output, $status_code); }
public function __construct($host, $port, $user, $passwd = "", $pulic_key = "", $private_key = "") { $this->host = $host; $this->connection = ssh2_connect($host, $port); if (empty($passwd)) { //resource $session , string $username , string $pubkeyfile , string $privkeyfile [, string $passphrase ] $ssh = ssh2_auth_pubkey_file($this->connection, $user, $pulic_key, $private_key); } else { if (empty($private_key) || empty($pulic_key)) { $ssh = ssh2_auth_password($this->connection, $user, $passwd); } } if ($ssh) { if (!($this->stdio = ssh2_shell($this->connection, "xterm"))) { throw new \Exception("GLI-014 : Connexion to ssh impossible on : " . $user . "@" . $host . ":" . $port . ""); } } else { echo "Connexion to ssh impossible on : " . $user . "@" . $host . ":" . $port . "\n"; //throw new \Exception("GLI-014 : Connexion to ssh impossible on : " . $user . "@" . $host . ":" . $port . ""); } }
public function auth($username, $public_key_path, $private_key_path, $throw_error = false) { //// // Confirm that the public key exists //// if (!file_exists($public_key_path)) { unset($this->ssh_connection); if ($throw_error) { //Throw error details (string) throw new Exception(Error::out(404, 'not found', 'The public SSH key file \'' . $public_key_path . '\' does not exist.')); } else { //Output error details Error::halt(404, 'not found', 'The public SSH key file \'' . $public_key_path . '\' does not exist.'); } } //// // Confirm that the private key exists //// if (!file_exists($private_key_path)) { unset($this->ssh_connection); if ($throw_error) { //Throw error details (string) throw new Exception(Error::out(404, 'not found', 'The private SSH key file \'' . $private_key_path . '\' does not exist.')); } else { //Output error details Error::halt(404, 'not found', 'The private SSH key file \'' . $private_key_path . '\' does not exist.'); } } if (!@ssh2_auth_pubkey_file($this->ssh_connection, $username, $public_key_path, $private_key_path)) { unset($this->ssh_connection); if ($throw_error) { //Throw error details (string) throw new Exception(Error::out(504, 'gateway timeout', 'Failed to authenticate a SSH connection with username \'' . $username . '\' on host \'' . $this->hostname . '\' on port \'' . $this->port . '\'.')); } else { //Output error details Error::halt(504, 'gateway timeout', 'Failed to authenticate a SSH connection with username \'' . $username . '\' on host \'' . $this->hostname . '\' on port \'' . $this->port . '\'.'); } } return true; }
function sync_yui($configuration) { system('./yuidoc.sh', $yui_exit); if ($yui_exit != 0) { fwrite(STDERR, "Failed to run yuidoc. Please check that it and its dependencies are installed.\n"); exit(11); } $port = isset($configuration->ssh->port) ? $configuration->ssh->port : DEFAULT_SSH_PORT; echo "Connecting to {$configuration->ssh->host} on port {$port}\n"; $con = ssh2_connect($configuration->ssh->host, $port); if (!$con) { fwrite(STDERR, "Failed to connect to {$configuration->ssh->host}\n"); exit(10); } if (isset($configuration->ssh->password)) { $auth_ret = ssh2_auth_password($con, $configuration->ssh->username, $configuration->ssh->password); if (!$auth_ret) { fwrite(STDERR, 'SSH password authentication failed.\\n'); exit(6); } } else { $passphrase = isset($configuration->ssh->passphrase) ? $configuration->ssh->passphrase : NULL; $auth_ret = ssh2_auth_pubkey_file($con, $configuration->ssh->username, $configuration->ssh->publicKeyFileName, $configuration->ssh->privateKeyFileName, $passphrase); if (!$auth_ret) { fwrite(STDERR, 'SSH public/private key authentication failed.\\n'); exit(15); } } $sftp = ssh2_sftp($con); if (!$sftp) { fwrite(STDERR, "Failed to open SFTP subsystem.\n"); exit(7); } sftp_walk($con, $sftp, 'yui_docs/html', $configuration->ssh->path); chdir(dirname(__FILE__)); echo "You have succesfully deployed the ProveIt API documentation.\n"; }
<?php $hid = $_POST[hid]; $cfile = file_get_contents('../config/config.php'); $l = explode("\n", $cfile); $l = $l[$hid]; $l = explode(" ", $l); $ip = $l[1]; $user = $l[2]; $host = $l[0]; $port = $l[3]; $connection = ssh2_connect("{$ip}", $port, array('hostkey' => 'ssh-rsa')); if (ssh2_auth_pubkey_file($connection, $user, '/var/lib/nginx/.ssh/id_rsa.pub', '/var/lib/nginx/.ssh/id_rsa')) { echo "Public Key Authentication Successful\n"; $sftp = ssh2_sftp($connection); $stream = fopen("ssh2.sftp://{$sftp}/var/spool/cron/{$user}", 'r'); $re = stream_get_contents($stream); $cont = explode("\n", $re); $num = -1; foreach ($cont as $line) { $patten = "/[\\s]+/"; $line = preg_split($patten, $line, 6); $com = $line[5]; $time = $line[0] . " " . $line[1] . " " . $line[2] . " " . $line[3] . " " . $line[4]; $num = $num + 1; $control = '<input class="bj_btn" type="button" value="edit" onclick="edit(this)"/> <input class="sj_btn" type="button" value="save" onclick="save(this)" /> <input class="del_btn" type="button" value="cancel" onclick="cancel(this)" />'; $timeedit = '<input type="text" style="display: none;width: 100%">'; $comedit = '<input type="text" style="display: none;width: 100%">'; echo '<tr class="tb_title">