use of com.axway.ats.core.ssh.exceptions.JschSshClientException in project ats-framework by Axway.
the class JschSshClient method connect.
/**
*
* @param user the user name
* @param password the user password
* @param host the target host
* @param port the specific port to use
* @param privateKey private key location. For example: ~/.ssh/id_rsa
* @param privateKeyPassword private key passphrase (or null if it hasn't)
*/
public void connect(String user, String password, String host, int port, String privateKey, String privateKeyPassword) {
try {
// disconnect if needed or stay connected if the host is the same
if (session != null && session.isConnected()) {
if (this.host.equals(host) && this.user.equals(user) && this.port == port) {
// already connected
return;
} else {
disconnect();
}
}
this.user = user;
this.host = host;
this.port = port;
JSch jsch = new JSch();
if (privateKey != null) {
jsch.addIdentity(privateKey, privateKeyPassword);
}
if (port > 0) {
session = jsch.getSession(user, host, port);
} else {
session = jsch.getSession(user, host);
}
session.setPassword(password);
// skip checking of known hosts and verifying RSA keys
session.setConfig("StrictHostKeyChecking", "no");
session.connect(CONNECTION_TIMEOUT);
} catch (Exception e) {
throw new JschSshClientException(e.getMessage() + "; Connection parameters are: user '" + user + "' at " + host + " on port " + port, e);
}
}
use of com.axway.ats.core.ssh.exceptions.JschSshClientException in project ats-framework by Axway.
the class JschSshClient method execute.
/**
*
* @param command SSH command to execute
* @return the exit code
*/
public int execute(String command, boolean waitForCompletion) {
try {
this.command = command;
execChannel = (ChannelExec) session.openChannel("exec");
execChannel.setCommand(command);
execChannel.setInputStream(null);
// Allocate a Pseudo-Terminal. Thus it supports login sessions. (eg. /bin/bash -l)
execChannel.setPty(true);
// there is a bug in the other method channel.connect( TIMEOUT );
execChannel.connect();
stdoutThread = new StreamReader(execChannel.getInputStream(), execChannel, "STDOUT");
stderrThread = new StreamReader(execChannel.getErrStream(), execChannel, "STDERR");
stdoutThread.start();
stderrThread.start();
if (waitForCompletion) {
stdoutThread.getContent();
stderrThread.getContent();
return execChannel.getExitStatus();
}
} catch (Exception e) {
throw new JschSshClientException(e.getMessage(), e);
} finally {
if (waitForCompletion && execChannel != null) {
execChannel.disconnect();
}
}
return -1;
}
Aggregations