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);
}
}
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();
}
}
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;
}
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;
}
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);
}
}
Aggregations