use of com.jcraft.jsch.JSch 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.jcraft.jsch.JSch in project ats-framework by Axway.
the class JschSftpClient method connect.
/**
* Create SFTP session connection
*
* @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 (this.session != null && this.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) {
this.session = jsch.getSession(user, host, port);
} else {
this.session = jsch.getSession(user, host);
}
this.session.setPassword(password);
// skip checking of known hosts and verifying RSA keys
this.session.setConfig("StrictHostKeyChecking", "no");
this.session.connect(CONNECTION_TIMEOUT);
this.channel = (ChannelSftp) this.session.openChannel("sftp");
// there is a bug in the other method channel.connect( TIMEOUT );
this.channel.connect();
} catch (Exception e) {
throw new JschSftpClientException(e.getMessage(), e);
}
}
use of com.jcraft.jsch.JSch in project azure-sdk-for-java by Azure.
the class ComputeManagementTest method ensureCanDoSsh.
protected void ensureCanDoSsh(String fqdn, int sshPort, String uname, String password) {
if (IS_MOCKED) {
return;
}
JSch jsch = new JSch();
com.jcraft.jsch.Session session = null;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session = jsch.getSession(uname, fqdn, sshPort);
session.setPassword(password);
session.setConfig(config);
session.connect();
} catch (Exception e) {
Assert.fail("SSH connection failed" + e.getMessage());
} finally {
if (session != null) {
session.disconnect();
}
}
}
use of com.jcraft.jsch.JSch in project gerrit by GerritCodeReview.
the class SshSession method getSession.
private Session getSession() throws JSchException {
if (session == null) {
JSch jsch = new JSch();
jsch.addIdentity("KeyPair", account.privateKey(), account.sshKey.getPublicKeyBlob(), null);
session = jsch.getSession(account.username, addr.getAddress().getHostAddress(), addr.getPort());
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
}
return session;
}
use of com.jcraft.jsch.JSch in project gerrit by GerritCodeReview.
the class GitUtil method initSsh.
public static void initSsh(final TestAccount a) {
final Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch.setConfig(config);
// register a JschConfigSessionFactory that adds the private key as identity
// to the JSch instance of JGit so that SSH communication via JGit can
// succeed
SshSessionFactory.setInstance(new JschConfigSessionFactory() {
@Override
protected void configure(Host hc, Session session) {
try {
final JSch jsch = getJSch(hc, FS.DETECTED);
jsch.addIdentity("KeyPair", a.privateKey(), a.sshKey.getPublicKeyBlob(), null);
} catch (JSchException e) {
throw new RuntimeException(e);
}
}
});
}
Aggregations