Search in sources :

Example 36 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 37 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 38 with JSch

use of com.jcraft.jsch.JSch in project MGit by maks.

the class PrivateKeyUtils method getPublicKeyEnsure.

public static File getPublicKeyEnsure(File privateKey) {
    File publicKey = getPublicKey(privateKey);
    if (!publicKey.exists()) {
        try {
            JSch jsch = new JSch();
            KeyPair kpair = KeyPair.load(jsch, privateKey.getAbsolutePath());
            kpair.writePublicKey(new FileOutputStream(publicKey), "mgit");
            kpair.dispose();
        } catch (Exception e) {
            // TODO
            e.printStackTrace();
        }
    }
    return publicKey;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) FileOutputStream(java.io.FileOutputStream) JSch(com.jcraft.jsch.JSch) File(java.io.File) IOException(java.io.IOException)

Example 39 with JSch

use of com.jcraft.jsch.JSch in project MGit by maks.

the class SGitSessionFactory method createDefaultJSch.

@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
    JSch jsch = new JSch();
    PrivateKeyUtils.migratePrivateKeys();
    File sshDir = PrivateKeyUtils.getPrivateKeyFolder();
    for (File file : sshDir.listFiles()) {
        KeyPair kpair = KeyPair.load(jsch, file.getAbsolutePath());
        jsch.addIdentity(file.getAbsolutePath());
    }
    return jsch;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) JSch(com.jcraft.jsch.JSch) File(java.io.File)

Example 40 with JSch

use of com.jcraft.jsch.JSch in project wildfly-camel by wildfly-extras.

the class EmbeddedSSHServer method setupKnownHosts.

private void setupKnownHosts() {
    // Add a localhost entry for the relevant host / port combination to known_hosts
    File knownHostsFile = homeDir.resolve("known_hosts").toFile();
    JSch jsch = new JSch();
    try {
        homeDir.toFile().mkdirs();
        knownHostsFile.createNewFile();
        jsch.setKnownHosts(knownHostsFile.getPath());
        Session s = jsch.getSession("admin", "localhost", sshServer.getPort());
        s.setConfig("StrictHostKeyChecking", "ask");
        s.setConfig("HashKnownHosts", "no");
        s.setUserInfo(new UserInfo() {

            @Override
            public String getPassphrase() {
                return null;
            }

            @Override
            public String getPassword() {
                return "admin";
            }

            @Override
            public boolean promptPassword(String message) {
                return true;
            }

            @Override
            public boolean promptPassphrase(String message) {
                return false;
            }

            @Override
            public boolean promptYesNo(String message) {
                return true;
            }

            @Override
            public void showMessage(String message) {
            }
        });
        s.connect();
        s.disconnect();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to configure known_hosts file", e);
    }
}
Also used : UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) File(java.io.File) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

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