Search in sources :

Example 16 with JSch

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);
    }
}
Also used : JschSshClientException(com.axway.ats.core.ssh.exceptions.JschSshClientException) JSch(com.jcraft.jsch.JSch) JschSshClientException(com.axway.ats.core.ssh.exceptions.JschSshClientException)

Example 17 with JSch

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);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) JSch(com.jcraft.jsch.JSch) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Example 18 with JSch

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();
        }
    }
}
Also used : JSch(com.jcraft.jsch.JSch) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 19 with JSch

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;
}
Also used : JSch(com.jcraft.jsch.JSch)

Example 20 with JSch

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);
            }
        }
    });
}
Also used : JSchException(com.jcraft.jsch.JSchException) Host(org.eclipse.jgit.transport.OpenSshConfig.Host) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) Properties(java.util.Properties) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Aggregations

JSch (com.jcraft.jsch.JSch)35 Session (com.jcraft.jsch.Session)23 JSchException (com.jcraft.jsch.JSchException)16 IOException (java.io.IOException)14 Channel (com.jcraft.jsch.Channel)8 ChannelExec (com.jcraft.jsch.ChannelExec)7 File (java.io.File)7 InputStream (java.io.InputStream)7 ChannelSftp (com.jcraft.jsch.ChannelSftp)6 FileInputStream (java.io.FileInputStream)6 OutputStream (java.io.OutputStream)6 UserInfo (com.jcraft.jsch.UserInfo)5 Properties (java.util.Properties)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 KeyPair (com.jcraft.jsch.KeyPair)2 SftpException (com.jcraft.jsch.SftpException)2 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)2 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)2 UserInfoPrompted (edu.umass.cs.aws.networktools.UserInfoPrompted)2 HeadlessException (java.awt.HeadlessException)2