function ssh_connect($host) { dbg_log("Connecting over SSH to {$host}"); #define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX); $ssh = new Net_SSH2($host); $key = new Crypt_RSA(); $key->setPassword(get_config()->host_ssh_private_key_password); $keyPath = get_config()->host_ssh_private_key; $keyString = file_get_contents($keyPath); $userString = get_config()->host_ssh_username; if (!$key->loadKey($keyString)) { dbg_log(var_dump($ssh->getErrors(), true)); exit("cannot import key {$keyPath}"); } if (!$ssh->login($userString, $key)) { dbg_log($ssh->getLastError()); exit('Login Failed'); } return $ssh; }
/** * Run this method after executing a command to detect errors * @param \Net_SSH2 $handle * @throws \Cogeco\Build\Exception */ protected static function checkCmdExceptions(\Net_SSH2 $handle) { if ($handle->getExitStatus() > 0) { throw new Exception("Command failed with exit status " . $handle->getExitStatus() . "\n\t" . implode("\n\t", $handle->getErrors())); } else { if (count($handle->getErrors())) { throw new Exception(__CLASS__ . ": Command failed.\n\t" . implode("\n\t", $handle->getErrors())); } } }
public function getErrors() { return $this->ssh->getErrors(); }
/** * @param array $config * * @return \Net_SSH2 * @throws \Exception */ private function createSSHHandler(array $config) { $ssh = new \Net_SSH2($config['hostname'], $config['port'], $config['timeout']); $password = $this->getPassword($config); if (empty($config['rsa_key'])) { $login = $ssh->login($config['username'], $password); $type = 'password'; } else { $login = $ssh->login($config['username'], $this->getKey($config, $password)); $type = 'key'; } if ($login !== true) { throw new \Exception(sprintf("Failed logging in to %s@%s:%d. Using type: %s. \nErrors: %s", $config['username'], $config['hostname'], $config['port'], $type, json_encode($ssh->getErrors(), true))); } return $ssh; }