public static boolean sshExecuteCmd(com.trilead.ssh2.Connection sshConnection, String cmd, int nTimes) { for (int i = 0; i < nTimes; i++) { try { final SSHCmdResult result = sshExecuteCmdOneShot(sshConnection, cmd); if (result.isSuccess()) { return true; } } catch (SshException ignored) { continue; } } return false; }
public static SSHCmdResult sshExecuteCmdWithResult(com.trilead.ssh2.Connection sshConnection, String cmd, int nTimes) { for (int i = 0; i < nTimes; i++) { try { final SSHCmdResult result = sshExecuteCmdOneShot(sshConnection, cmd); if (result.isSuccess()) { return result; } } catch (SshException ignored) { continue; } } return new SSHCmdResult(-1, null, null); }
/** * @param publicKey * @return */ private String getFingerprint(final String publicKey) { final String fingerprint = SSHKeysHelper.getPublicKeyFingerprint(publicKey); return fingerprint; }
public Pair<Boolean, String> executeSshWrapper(final String hostIp, final int port, final String username, final File pemFile, final String hostPasswd, final String command) throws Exception { final Pair<Boolean, String> result = SshHelper.sshExecute(hostIp, port, username, pemFile, hostPasswd, command, 60000, 60000, TIMEOUT); return result; }
public String getPublicKeyFingerPrint() { return getPublicKeyFingerprint(getPublicKey()); }
public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, byte[] data, String remoteFileName, String fileMode) throws Exception { scpTo(host, port, user, pemKeyFile, password, remoteTargetDirectory, data, remoteFileName, fileMode, DEFAULT_CONNECT_TIMEOUT, DEFAULT_KEX_TIMEOUT); }
public static boolean sshExecuteCmd(com.trilead.ssh2.Connection sshConnection, String cmd) { return sshExecuteCmd(sshConnection, cmd, 3); }
public static com.trilead.ssh2.Connection acquireAuthorizedConnection(String ip, String username, String password) { return acquireAuthorizedConnection(ip, 22, username, password); }
@Override public String toString() { return String.format("SSH cmd result: return code=%d, stdout=%s, stderr=%s", getReturnCode(), getStdOut().split("-----BEGIN")[0], getStdErr()); }
@Test public void doNotThrowSshExceptionIfConditionsStdout() throws SshException { SshHelper.throwSshExceptionIfConditionsTimeout(ChannelCondition.STDOUT_DATA); }
@Test(expected = SshException.class) public void throwSshExceptionIfStdoutOrStdeerIsNullTestNull() throws SshException { SshHelper.throwSshExceptionIfStdoutOrStdeerIsNull(null, null); }
/** * @param key * @return * @throws InvalidParameterValueException */ protected String getPublicKeyFromKeyKeyMaterial(final String key) throws InvalidParameterValueException { final String publicKey = SSHKeysHelper.getPublicKeyFromKeyMaterial(key); if (publicKey == null) { throw new InvalidParameterValueException("Public key is invalid"); } return publicKey; }
/** * It throws a {@link SshException} if the channel condition is {@link ChannelCondition#TIMEOUT} */ protected static void throwSshExceptionIfConditionsTimeout(int conditions) throws SshException { if ((conditions & ChannelCondition.TIMEOUT) != 0) { String msg = "Timed out in waiting for SSH execution exit status"; s_logger.error(msg); throw new SshException(msg); } }
public static SSHCmdResult sshExecuteCmdWithResult(com.trilead.ssh2.Connection sshConnection, String cmd) { return sshExecuteCmdWithResult(sshConnection, cmd, 3); }
public void disconnect() { if (_session != null) { _session.close(); } SSHCmdHelper.releaseSshConnection(_connection); }
private static void deleteKeyFile(final String nsIp, final String username, final String password, final String keyFilename) throws Exception { SshHelper.sshExecute(nsIp, SSH_PORT, username, null, password, "shell rm " + SSL_CERT_PATH + keyFilename); }
public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, String localFile, String fileMode) throws Exception { scpTo(host, port, user, pemKeyFile, password, remoteTargetDirectory, localFile, fileMode, DEFAULT_CONNECT_TIMEOUT, DEFAULT_KEX_TIMEOUT); }
/** * Checks if the SSH session {@link com.trilead.ssh2.Session#getStdout()} or * {@link com.trilead.ssh2.Session#getStderr()} is null. */ protected static void throwSshExceptionIfStdoutOrStdeerIsNull(InputStream stdout, InputStream stderr) throws SshException { if (stdout == null || stderr == null) { String msg = "Stdout or Stderr of SSH session is null"; s_logger.error(msg); throw new SshException(msg); } } }
public static Pair<Boolean, String> sshExecute(String host, int port, String user, File pemKeyFile, String password, String command) throws Exception { return sshExecute(host, port, user, pemKeyFile, password, command, DEFAULT_CONNECT_TIMEOUT, DEFAULT_KEX_TIMEOUT, 120000); }
private static void deleteCertFile(final String nsIp, final String username, final String password, final String certFilename) throws Exception { SshHelper.sshExecute(nsIp, SSH_PORT, username, null, password, "shell rm " + SSL_CERT_PATH + certFilename); }