Search in sources :

Example 66 with JSch

use of com.jcraft.jsch.JSch in project tigervnc by TigerVNC.

the class Tunnel method createTunnelJSch.

private static void createTunnelJSch(String gatewayHost, String remoteHost, int remotePort, int localPort) throws Exception {
    JSch.setLogger(new MyJSchLogger());
    JSch jsch = new JSch();
    try {
        // NOTE: jsch does not support all ciphers.  User may be
        // prompted to accept host key authenticy even if
        // the key is in the known_hosts file.
        File knownHosts = new File(FileUtils.getHomeDir() + ".ssh" + FileUtils.getFileSeparator() + "known_hosts");
        if (knownHosts.exists() && knownHosts.canRead())
            jsch.setKnownHosts(knownHosts.getAbsolutePath());
        ArrayList<File> privateKeys = new ArrayList<File>();
        if (!getSshKey().isEmpty()) {
            byte[] keyPass = null, key;
            if (!sshKeyPass.getValue().isEmpty())
                keyPass = sshKeyPass.getValue().getBytes();
            jsch.addIdentity("TigerVNC", getSshKey().getBytes(), null, keyPass);
        } else if (!getSshKeyFile().isEmpty()) {
            File f = new File(getSshKeyFile());
            if (!f.exists() || !f.canRead())
                throw new Exception("Cannot access SSH key file " + getSshKeyFile());
            privateKeys.add(f);
        }
        for (Iterator<File> i = privateKeys.iterator(); i.hasNext(); ) {
            File privateKey = (File) i.next();
            if (privateKey.exists() && privateKey.canRead())
                if (!sshKeyPass.getValue().isEmpty())
                    jsch.addIdentity(privateKey.getAbsolutePath(), sshKeyPass.getValue());
                else
                    jsch.addIdentity(privateKey.getAbsolutePath());
        }
        String user = getSshUser();
        String label = new String("SSH Authentication");
        PasswdDialog dlg = new PasswdDialog(label, (user == null ? false : true), false);
        dlg.userEntry.setText(user != null ? user : "");
        File ssh_config = new File(sshConfig.getValue());
        if (ssh_config.exists() && ssh_config.canRead()) {
            ConfigRepository repo = OpenSSHConfig.parse(ssh_config.getAbsolutePath());
            jsch.setConfigRepository(repo);
        }
        Session session = jsch.getSession(user, gatewayHost, getSshPort());
        session.setUserInfo(dlg);
        // OpenSSHConfig doesn't recognize StrictHostKeyChecking
        if (session.getConfig("StrictHostKeyChecking") == null)
            session.setConfig("StrictHostKeyChecking", "ask");
        session.connect();
        if (gatewayHost.equals(remoteHost))
            session.setPortForwardingL(localPort, new String("localhost"), remotePort);
        else
            session.setPortForwardingL(localPort, remoteHost, remotePort);
    } catch (java.lang.Exception e) {
        throw new Exception(e.getMessage());
    }
}
Also used : ConfigRepository(com.jcraft.jsch.ConfigRepository) JSch(com.jcraft.jsch.JSch) IOException(java.io.IOException) Exception(com.tigervnc.rfb.Exception) JSchException(com.jcraft.jsch.JSchException) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 67 with JSch

use of com.jcraft.jsch.JSch in project winery by eclipse.

the class InstanceModelUtils method createJschSession.

public static Session createJschSession(TTopologyTemplate template, List<String> nodeIdsToBeReplaced) {
    Map<String, String> sshCredentials = getSSHCredentials(template, nodeIdsToBeReplaced);
    try {
        JSch jsch = new JSch();
        File key = File.createTempFile("key", "tmp", FileUtils.getTempDirectory());
        FileUtils.write(key, sshCredentials.get(vmPrivateKey), "UTF-8");
        logger.info("tmp key file created: {}", key.exists());
        jsch.addIdentity(key.getAbsolutePath());
        Session session = sshCredentials.containsKey(vmSshPort) ? jsch.getSession(sshCredentials.get(vmUser), sshCredentials.get(vmIP), Integer.parseInt(sshCredentials.get(vmSshPort))) : jsch.getSession(sshCredentials.get(vmUser), sshCredentials.get(vmIP));
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        FileUtils.forceDelete(key);
        logger.info("tmp key file deleted: {}", key.exists());
        return session;
    } catch (JSchException | IOException e) {
        logger.error("Failed to connect to {} using user {}.", sshCredentials.get(vmIP), sshCredentials.get(vmUser), e);
        throw new RuntimeException(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 68 with JSch

use of com.jcraft.jsch.JSch in project suite by stupidsing.

the class Ssh method session.

private <T> T session(String host, int port, String user, String password, SshFun<Session, T> fun) throws IOException, SftpException, JSchException {
    var jsch = new JSch();
    var config = new Properties();
    config.setProperty("StrictHostKeyChecking", "no");
    var session = jsch.getSession(user, host, port);
    session.setUserInfo(new UserInfo() {

        public String getPassphrase() {
            return null;
        }

        public String getPassword() {
            return password;
        }

        public boolean promptPassphrase(String arg0) {
            return true;
        }

        public boolean promptPassword(String arg0) {
            return true;
        }

        public boolean promptYesNo(String arg0) {
            return true;
        }

        public void showMessage(String arg0) {
        }
    });
    session.setConfig(config);
    session.connect();
    try {
        return fun.apply(session);
    } finally {
        session.disconnect();
    }
}
Also used : UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties)

Example 69 with JSch

use of com.jcraft.jsch.JSch in project pentaho-kettle by pentaho.

the class SftpServer method createJschSession.

public Session createJschSession() throws JSchException {
    JSch jsch = new JSch();
    com.jcraft.jsch.Session session = jsch.getSession(username, server.getHost(), server.getPort());
    session.setPassword(password);
    Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    return session;
}
Also used : Session(com.jcraft.jsch.Session) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties)

Example 70 with JSch

use of com.jcraft.jsch.JSch in project fabric8 by jboss-fuse.

the class GitUtils method configureCommand.

/**
 * Configures the transport of the command to deal with things like SSH
 */
public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command, CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey) {
    LOG.info("Using " + credentialsProvider);
    if (sshPrivateKey != null) {
        final CredentialsProvider provider = credentialsProvider;
        command.setTransportConfigCallback(new TransportConfigCallback() {

            @Override
            public void configure(Transport transport) {
                if (transport instanceof SshTransport) {
                    SshTransport sshTransport = (SshTransport) transport;
                    SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {

                        @Override
                        protected void configure(OpenSshConfig.Host host, Session session) {
                            session.setConfig("StrictHostKeyChecking", "no");
                            UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
                            session.setUserInfo(userInfo);
                        }

                        @Override
                        protected JSch createDefaultJSch(FS fs) throws JSchException {
                            JSch jsch = super.createDefaultJSch(fs);
                            jsch.removeAllIdentity();
                            String absolutePath = sshPrivateKey.getAbsolutePath();
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Adding identity privateKey: " + sshPrivateKey + " publicKey: " + sshPublicKey);
                            }
                            if (sshPublicKey != null) {
                                jsch.addIdentity(absolutePath, sshPublicKey.getAbsolutePath(), null);
                            } else {
                                jsch.addIdentity(absolutePath);
                            }
                            return jsch;
                        }
                    };
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                }
            }
        });
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) UserInfo(com.jcraft.jsch.UserInfo) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) OpenSshConfig(org.eclipse.jgit.transport.OpenSshConfig) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) SshTransport(org.eclipse.jgit.transport.SshTransport) Session(com.jcraft.jsch.Session) SSLSession(javax.net.ssl.SSLSession)

Aggregations

JSch (com.jcraft.jsch.JSch)130 Session (com.jcraft.jsch.Session)72 JSchException (com.jcraft.jsch.JSchException)51 IOException (java.io.IOException)50 Channel (com.jcraft.jsch.Channel)35 File (java.io.File)29 InputStream (java.io.InputStream)29 Properties (java.util.Properties)27 ChannelExec (com.jcraft.jsch.ChannelExec)26 ChannelSftp (com.jcraft.jsch.ChannelSftp)22 KeyPair (com.jcraft.jsch.KeyPair)19 BufferedReader (java.io.BufferedReader)16 UserInfo (com.jcraft.jsch.UserInfo)15 InputStreamReader (java.io.InputStreamReader)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 FileInputStream (java.io.FileInputStream)11 OutputStream (java.io.OutputStream)11 SftpException (com.jcraft.jsch.SftpException)10 FS (org.eclipse.jgit.util.FS)8 FileOutputStream (java.io.FileOutputStream)7