@Override public boolean setIdentity(String privateKey, JSch jsch) { try { jsch.addIdentity(privateKey); log.info("Successfully set identity using local file " + privateKey); return true; } catch (Exception e) { log.warn("Failed to set identity using local file. Will attempt next strategy. " + e.getMessage()); } return false; } }
private Session newJSchSession() throws JSchException { JSch jsch = new JSch(); if (identityPath != null) { jsch.addIdentity(identityPath); } Session session = jsch.getSession(username, hostname, port); if (password != null) { session.setPassword(password); } session.setConfig("StrictHostKeyChecking", "no"); return session; }
@Override public boolean setIdentity(String privateKey, JSch jsch) { FileSystem fs; try { fs = FileSystem.get(new Configuration()); } catch (Exception e) { log.warn("Failed to set identity using HDFS file. Will attempt next strategy. " + e.getMessage()); return false; } Preconditions.checkNotNull(fs, "FileSystem cannot be null"); try (FSDataInputStream privateKeyStream = fs.open(new Path(privateKey))) { byte[] bytes = IOUtils.toByteArray(privateKeyStream); jsch.addIdentity("sftpIdentityKey", bytes, (byte[]) null, (byte[]) null); log.info("Successfully set identity using HDFS file"); return true; } catch (Exception e) { log.warn("Failed to set identity using HDFS file. Will attempt next strategy. " + e.getMessage()); return false; } } }
private Session createSession(String host, Args args) throws JSchException { JSch jsch = new JSch(); for (String keyFile : getKeyFiles()) { jsch.addIdentity(keyFile); } JSch.setLogger(new LogAdapter()); Session session = jsch.getSession(args.user, host, args.sshPort); session.setConfig("StrictHostKeyChecking", "no"); return session; }
String privateKey = ".ssh/id_rsa"; jsch.addIdentity(privateKey); System.out.println("identity added ");
@Override protected JSch createDefaultJSch(FS fs) throws JSchException { if (GitMonitoringService.this.isJschLoggerEnabled) { JSch.setLogger(new JschLogger()); } JSch defaultJSch = super.createDefaultJSch(fs); defaultJSch.getIdentityRepository().removeAll(); if (GitMonitoringService.this.privateKeyPath != null) { defaultJSch.addIdentity(GitMonitoringService.this.privateKeyPath, GitMonitoringService.this.passphrase); } else { defaultJSch.addIdentity("gaas-git", GitMonitoringService.this.privateKey, null, GitMonitoringService.this.passphrase.getBytes(Charset.forName("UTF-8"))); } if (!Strings.isNullOrEmpty(GitMonitoringService.this.knownHosts)) { defaultJSch.setKnownHosts(new ByteArrayInputStream(GitMonitoringService.this.knownHosts.getBytes(Charset.forName("UTF-8")))); } else if (!Strings.isNullOrEmpty(GitMonitoringService.this.knownHostsFile)) { defaultJSch.setKnownHosts(GitMonitoringService.this.knownHostsFile); } return defaultJSch; } };
private void setSession() { JSch ssh; // wait until SSH port of remote end comes up boolean success = waitActive( SESSION_CONNECT_TIMEOUT ); if( ! success ) { LOG.warn( "Port 22 of {} did not open in time", value.getPublicIpAddress() ); } // try to open ssh session try { Thread.sleep( 30000 ); ssh = new JSch(); ssh.addIdentity( value.getSshKeyFile() ); session = ssh.getSession( Utils.DEFAULT_USER, value.getPublicIpAddress() ); session.setConfig( "StrictHostKeyChecking", "no" ); session.connect(); } catch ( Exception e ) { LOG.error( "Error while connecting to ssh session of " + value.getPublicIpAddress(), e ); session = null; } }
public static Session createSession(final SFTPConfiguration conf, final JSch jsch) throws JSchException, IOException { if (conf == null || null == jsch) { throw new NullPointerException(); } final Hashtable<String, String> newOptions = new Hashtable<>(); Session session = jsch.getSession(conf.username, conf.hostname, conf.port); final String hostKeyVal = conf.hostkeyFile; if (null != hostKeyVal) { try { jsch.setKnownHosts(hostKeyVal); } catch (final IndexOutOfBoundsException iob) { throw new IOException("Unable to establish connection due to bad known hosts key file " + hostKeyVal, iob); } } else { newOptions.put("StrictHostKeyChecking", "no"); session.setConfig(newOptions); } final String privateKeyVal = conf.privatekeyFile; if (null != privateKeyVal) { jsch.addIdentity(privateKeyVal, conf.privateKeypassphrase); } if (null != conf.password) { session.setPassword(conf.password); } session.setTimeout(conf.connectionTimeout); //set timeout for connection session.connect(); session.setTimeout(conf.dataTimeout); //set timeout for data transfer return session; }
jsch.addIdentity(keyFile);
@Override protected Session createSession(Host hc, String user, String host, int port, FS fs) throws JSchException { if (sshKeysByHostname.containsKey(host)) { JGitEnvironmentProperties sshUriProperties = sshKeysByHostname.get(host); jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null, null); if (sshUriProperties.getKnownHostsFile() != null) { jSch.setKnownHosts(sshUriProperties.getKnownHostsFile()); } if (sshUriProperties.getHostKey() != null) { HostKey hostkey = new HostKey(host, Base64.decode(sshUriProperties.getHostKey())); jSch.getHostKeyRepository().add(hostkey, null); } return jSch.getSession(user, host, port); } throw new JSchException("no keys configured for hostname " + host); }
passphrasebytes = GetPrivateKeyPassPhrase().getBytes(); jsch.addIdentity( getUserName(), FileUtil.getContent( KettleVFS.getFileObject( prvkey ) ), // byte[] privateKey
jsch.addIdentity(privateKeyFile, ctx.getProperty(PRIVATE_KEY_PASSPHRASE).evaluateAttributeExpressions(flowFile).getValue());
private static void loadIdentity(JSch sch, File priv) { if (priv.isFile()) { try { sch.addIdentity(priv.getAbsolutePath()); } catch (JSchException e) { // Instead, pretend the key doesn't exist. } } }
ssh.addIdentity(spec.key().getAbsolutePath());
private Session newJSchSession() throws JSchException { JSch jsch = new JSch(); if (identityPath != null) { jsch.addIdentity(identityPath); } Session session = jsch.getSession(username, hostname, 22); if (password != null) { session.setPassword(password); } session.setConfig("StrictHostKeyChecking", "no"); return session; }
@Override protected JSch createDefaultJSch(FS fs) throws JSchException { JSch jSch = super.createDefaultJSch(fs); // apply customized private key if (privateKeyPath != null) { jSch.removeAllIdentity(); jSch.addIdentity(privateKeyPath.toString()); } return jSch; } };
jsch.addIdentity(privKeyAbsPath, sshMeta.getPassphrase()); } else { jsch.addIdentity(privKeyAbsPath);
Session session = jsch.getSession(proxyUser, proxyHost); if (AuthenticationMethod.PRIVATE_KEY.equals(authenticationMethod)) { jsch.addIdentity(sshTunnelingConfiguration.getPrivateKeyPath(), sshTunnelingConfiguration.getProxyPassword()); } else {
private ChannelSftp openSSHConnection(Server server) throws Exception { logger.debug("Opening SSH connection to {}@{}", server.getUsername(), server.getHostname()); JSch jsch = new JSch(); Session session = jsch.getSession(server.getUsername(), server.getHostname(), server.getPort()); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); if (server.getPemPath() != null) { jsch.addIdentity(server.getPemPath()); } session.setConfig(config); if (server.getPassword() != null) { session.setPassword(server.getPassword()); } session.connect(); //Open a new session for SFTP. Channel channel = session.openChannel("sftp"); channel.connect(); //checking SSH client connection. if (!channel.isConnected()) { logger.warn("Cannot connect with SSH to {}@{}", server.getUsername(), server.getHostname()); throw new RuntimeException("Can not connect to " + server.getUsername() + "@" + server.getHostname()); } logger.debug("SSH connection successful"); return (ChannelSftp) channel; } }
jsch.addIdentity(identityKey); byIdentityFile.put(identityKey, jsch);