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