Example #1
1
 public function setUp()
 {
     /*
      * Create an ssh connection
      */
     $session = ssh2_connect(getenv("SSH_HOST"), getenv("SSH_PORT"), array('hostkey' => 'ssh-rsa')) or die("Couldn't connect.\n");
     ssh2_auth_agent($session, getenv("SSH_USER")) or die("Couldn't authenticate. You might need to: eval `ssh-agent -s` && ssh-add\n");
     $this->session = $session;
 }
Example #2
1
 private function buildSshConnection($sshConfig)
 {
     $c = @ssh2_connect($sshConfig['host'], 22, array('hostkey' => 'ssh-rsa'));
     if (!is_resource($c)) {
         throw new \Exception("Could not connect to host: {$sshConfig['host']}");
     }
     $sshAuthSuccess = @ssh2_auth_agent($c, $sshConfig['username']);
     if (!$sshAuthSuccess) {
         throw new \Exception("Could not authenticate on host: {$sshConfig['host']}");
     }
     return $c;
 }
Example #3
0
 public function setUp()
 {
     /*
      * Create source connection
      */
     $sourceSsh = ssh2_connect(getenv("SOURCE_SSH_HOST"), getenv("SOURCE_SSH_PORT"), array('hostkey' => 'ssh-rsa'));
     if (!is_resource($sourceSsh)) {
         $this->markTestSkipped("Could not connect to source.");
     }
     if (!@ssh2_auth_agent($sourceSsh, getenv("SOURCE_SSH_USER"))) {
         $this->markTestSkipped("Couldn't authenticate on source. You might need to: eval `ssh-agent -s` && ssh-add");
     }
     $this->sourceSsh = $sourceSsh;
     /*
      * Create destination connection
      */
     $destSsh = ssh2_connect(getenv("DEST_SSH_HOST"), getenv("DEST_SSH_PORT"), array('hostkey' => 'ssh-rsa'));
     if (!is_resource($destSsh)) {
         $this->markTestSkipped("Could not connect to destination.");
     }
     if (!@ssh2_auth_agent($destSsh, getenv("DEST_SSH_USER"))) {
         $this->markTestSkipped("Couldn't authenticate on destination. You might need to: eval `ssh-agent -s` && ssh-add");
     }
     $this->destSsh = $destSsh;
 }
Example #4
0
 public function authByAgent($user)
 {
     if (!function_exists('ssh2_auth_agent')) {
         throw new \Exception("ssh2_auth_agent does not exists");
     }
     return ssh2_auth_agent($this->connection, $user);
 }
Example #5
0
 /**
  * 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']);
         } else {
             ssh2_auth_agent($this->connection, $parts['user']);
         }
         $this->sftp = ssh2_sftp($this->connection);
     });
 }
Example #6
0
 protected function _createSshSession()
 {
     $host = $this->getHost();
     $user = $this->getUser();
     $sshSession = ssh2_connect($host, 22, array('hostkey' => 'ssh-dss'));
     if ($sshSession) {
         if (ssh2_auth_agent($sshSession, $user)) {
             return $sshSession;
         } else {
             throw new Exception("Could not authenticate a session to {$host} using the SSH agent.");
         }
     } else {
         throw new Exception("Could not connect to {$host}.");
     }
 }
 /**
  * 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);
     });
 }
Example #8
0
 /**
  * @param array $server
  */
 public function connect(array $server)
 {
     $this->stream = ssh2_connect($server['host'], $server['port']);
     if (!$this->stream) {
         throw new \InvalidArgumentException(sprintf('SSH connection failed on "%s:%s"', $server['host'], $server['ssh_port']));
     }
     if (array_key_exists('password', $server)) {
         if (!ssh2_auth_password($this->stream, $server['username'], $server['password'])) {
             throw new \InvalidArgumentException(sprintf('SSH authentication failed for user "%s"', $server['username']));
         }
     } else {
         if (!ssh2_auth_agent($this->stream, $server['username'])) {
             throw new \InvalidArgumentException(sprintf('SSH connection failed on "%s" with username %s', $server['host'], $server['username']));
         }
     }
     $this->shell = ssh2_shell($this->stream);
     if (!$this->shell) {
         throw new \RuntimeException('Failed opening shell');
     }
     $this->output->writeln('<info>Connected to the server</info>');
 }
 public function setUp()
 {
     /*
      * Create dest ssh connection
      */
     $destSsh = @ssh2_connect(getenv("SSH_HOST"), getenv("SSH_PORT"), array('hostkey' => 'ssh-rsa'));
     if (!is_resource($destSsh)) {
         $this->markTestSkipped("Couldn't connect to dest.");
     }
     if (!@ssh2_auth_agent($destSsh, getenv("SSH_USER"))) {
         $this->markTestSkipped("Couldn't authenticate dest. You might need to: eval `ssh-agent -s` && ssh-add");
     }
     $this->destSsh = $destSsh;
     /*
      * Create source ssh connection
      */
     $sourceSsh = @ssh2_connect(getenv("SSH_HOST"), getenv("SSH_PORT"), array('hostkey' => 'ssh-rsa'));
     if (!is_resource($sourceSsh)) {
         $this->markTestSkipped("Couldn't connect to source.");
     }
     if (!@ssh2_auth_agent($sourceSsh, getenv("SSH_USER"))) {
         $this->markTestSkipped("Couldn't authenticate source. You might need to: eval `ssh-agent -s` && ssh-add");
     }
     $this->sourceSsh = $sourceSsh;
     /*
      * Set up the local dest database connection
      */
     try {
         $this->destDbh = $destDbh = new \PDO(sprintf("mysql:host=%s;dbname=%s", $this->getMachineParams()['local']['dest']['db']['host'], $this->getMachineParams()['local']['dest']['db']['name']), $this->getMachineParams()['local']['dest']['db']['username'], $this->getMachineParams()['local']['dest']['db']['password']);
     } catch (\Exception $e) {
         $this->markTestSkipped("Couldn't connect to local dest database: {$e}");
         return;
     }
     /*
      * Set up the local source database
      */
     try {
         $this->sourceDbh = $sourceDbh = new \PDO(sprintf("mysql:host=%s;dbname=%s", $this->getMachineParams()['local']['source']['db']['host'], $this->getMachineParams()['local']['source']['db']['name']), $this->getMachineParams()['local']['source']['db']['username'], $this->getMachineParams()['local']['source']['db']['password']);
     } catch (\Exception $e) {
         $this->markTestSkipped("Couldn't connect to local source database: {$e}");
         return;
     }
     $sourceDbh->query("\n            CREATE TABLE wp_deploy_synctest (\n                id  INT UNSIGNED  NOT NULL  AUTO_INCREMENT,\n                name VARCHAR(255) NULL,\n                content TEXT NULL,\n                PRIMARY KEY (id)\n            )\n        ") or $this->markTestSkipped("Couldn't create local source test table.");
     // Insert some database values
     $sourceDbh->query("INSERT INTO wp_deploy_synctest (name, content) VALUES ('test_value_one', 'test_content_one')") or $this->markTestSkipped("Couldn't add data to local source test table.");
     /*
      * Set up remote source database
      */
     $remoteSourceCmd = new Ssh\Command($this->sourceSsh);
     $remoteMysql = CommandUtil::buildMysqlCommand($this->getMachineParams()['remote']['source']['db']);
     $remoteCreateCommand = "{$remoteMysql} -e 'CREATE TABLE wp_deploy_synctest (\n                id  INT UNSIGNED  NOT NULL  AUTO_INCREMENT,\n                name VARCHAR(255) NULL,\n                content TEXT NULL,\n                PRIMARY KEY (id)\n            )'";
     $remoteSourceCmd->exec($remoteCreateCommand);
     if ($remoteSourceCmd->failure()) {
         $this->markTestSkipped("Couldn't create remote source test table.");
     }
     // Insert some database values
     $destInsertCommand = "{$remoteMysql} -e 'INSERT INTO wp_deploy_synctest (name, content) VALUES (\"test_value_one\", \"test_content_one\")'";
     $remoteSourceCmd->exec($destInsertCommand);
     if ($remoteSourceCmd->failure()) {
         $this->markTestSkipped("Couldn't insert data into remote source test table.");
     }
 }
Example #10
0
 /**
  * {@inheritDoc}
  */
 public function authenticate($session)
 {
     return ssh2_auth_agent($session, $this->username);
 }
Example #11
0
 protected function _openSshSession($host)
 {
     $session = ssh2_connect($host, 22, array('hostkey' => 'ssh-dss'));
     ssh2_auth_agent($session, $this->getUser());
     return $session;
 }
 /**
  * Run SSH authentication based on the selected method
  *
  * @param resource $connection
  * @param int $method
  * @param mixed ...$args
  *
  * @return void
  */
 protected function __authenticate($connection, $method, ...$args)
 {
     switch ($method) {
         case SecureShell::AUTH_PASSWORD:
             return ssh2_auth_password($connection, ...$args);
         case SecureShell::AUTH_PUBKEY:
             return ssh2_auth_pubkey_file($connection, ...$args);
         case SecureShell::AUTH_HOSTKEY:
             return ssh2_auth_hostbased_file($connection, ...$args);
         case SecureShell::AUTH_AGENT:
             return ssh2_auth_agent($connection, ...$args);
         case SecureShell::AUTH_NONE:
             return ssh2_auth_none($connection, ...$args);
         default:
             throw new ModuleException(__CLASS__, 'Unsupported authentication method');
     }
 }