@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; } };
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; }
Session session = null; Channel channel = null; try { JSch ssh = new JSch(); ssh.setKnownHosts("/path/of/known_hosts/file"); session = ssh.getSession("username", "host", 22); session.setPassword("password"); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftp = (ChannelSftp) channel; sftp.put("/path/of/local/file", "/path/of/ftp/file"); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } finally { if (channel != null) { channel.disconnect(); } if (session != null) { session.disconnect(); } }
jsch.setKnownHosts( knownHostsFilename );
@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); }
this.session.setConfig("StrictHostKeyChecking", "no"); } else { jsch.setKnownHosts(knownHosts);
jsch.setKnownHosts(hostKeyVal);
private static void knownHosts(JSch sch, FS fs) throws JSchException { final File home = fs.userHome(); if (home == null) return; final File known_hosts = new File(new File(home, ".ssh"), "known_hosts"); //$NON-NLS-1$ //$NON-NLS-2$ try (FileInputStream in = new FileInputStream(known_hosts)) { sch.setKnownHosts(in); } catch (FileNotFoundException none) { // Oh well. They don't have a known hosts in home. } catch (IOException err) { // Oh well. They don't have a known hosts in home. } }
this.jsch.setKnownHosts(this.knownHosts);
/** * adds server rsa key to known hosts */ public void setKnownHost() throws JSchException { jschSSHChannel.setKnownHosts(new ByteArrayInputStream(this.rsaKey.getBytes(UTF_8))); }
JSch jsch = new JSch(); <br> jsch.setKnownHosts(myKonfig.getKnownHostsFile()); <br> String privKeyFile = myKonfig.getPrivateKeyFile(); <br> jsch.addIdentity(privKeyFile); <br> </br> </br> </br> </br>
JSch jsch = new JSch(); jsch.setKnownHosts(knownHostsFile); logger.info("known hosts file set: " + knownHostsFile); jsch.addIdentity(privateKey); logger.info("rsa private key loaded: " + privateKey); Session session = jsch.getSession(user, host, port); java.util.Properties config = new java.util.Properties(); // this setting will cause JSCH to automatically add all target servers' entry to the known_hosts file config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect();
protected static void knownHosts(final JSch jsch, FS fs) throws JSchException { final File home = fs.userHome(); if (home == null) return; final Path knownHosts = home.toPath().resolve(HSSH.SSHDIR).resolve(HSSH.KNOWN_HOSTS); try { try (final InputStream in = Files.newInputStream(knownHosts)) { jsch.setKnownHosts(in); } } catch (IOException exception) { // No known hosts file, no real problem } }
JSch jsch=new JSch(); Session session=jsch.getSession("my_username", "my_host", my_port); session.setConfig("PreferredAuthentications", "publickey"); jsch.setKnownHosts("~/.ssh/known_hosts"); jsch.addIdentity("~/.ssh/id_rsa"); session.setConfig("StrictHostKeyChecking", "no"); session.connect(30000); Channel channel=session.openChannel("shell"); channel.setInputStream(System.in); channel.setOutputStream(System.out); channel.connect(3*1000);
private static void doCommonConstructorActions(String userName, String password, String connectionIP, String knownHostsFileName) { jschSSHChannel = new JSch(); try { jschSSHChannel.setKnownHosts(knownHostsFileName); } catch (JSchException jschX) { LOGGER.error(jschX.getMessage()); errorMessage = jschX.getMessage(); } strUserName = userName; strPassword = password; strConnectionIP = connectionIP; }
protected JSch getJSch() throws JSchException { JSch jsch = getJSch(privateKeys, privateKeyStrings); if (strictHostKeyChecking && knownHosts.exists()) { String path = LocationUtils.getCanonicalPath(knownHosts); jsch.setKnownHosts(path); } return jsch; }
protected JSch getJSch() throws JSchException { List<File> uniquePrivateKeyFiles = getUniquePrivateKeyFiles(); logger.debug("Located {} private keys on the file system", uniquePrivateKeyFiles.size()); JSch jsch = getJSch(uniquePrivateKeyFiles, privateKeyStrings); if (strictHostKeyChecking && knownHosts != null) { String path = LocationUtils.getCanonicalPath(knownHosts); jsch.setKnownHosts(path); } return jsch; }
@Override protected boolean initConnection() { boolean success = true; try { jsch.setKnownHosts(SshManager.getInstance().getKnownHosts().getAbsolutePath()); jsch.addIdentity(SshManager.getInstance().getPrivateKey().getAbsolutePath()); } catch (IOException | JSchException e) { logger.error(e.getMessage().replace("\n", " ")); success = false; } return success; }
protected com.jcraft.jsch.JSch getJSch(OpenSshConfig.Host hc, FS fs) throws com.jcraft.jsch.JSchException { JSch jsch = super.getJSch(hc, fs); JSch.setConfig("StrictHostKeyChecking", "no"); if(FileSystemManager.exists(privateKeyFile)) { jsch.addIdentity(privateKeyFile); } jsch.setKnownHosts(knownHostsFile); return jsch; }
private void login() throws Exception { provider.connect(); verify(jsch).setKnownHosts(hostFile.getAbsolutePath()); verify(session).setTimeout(new Long(SECONDS.toMillis(TIMEOUT)).intValue()); verify(session).connect(); verify(channel).connect(); Properties properties = captureLoginProperties(); assertThat(properties.getProperty(PREFERRED_AUTHENTICATION_METHODS), equalTo(GSSAPI_WITH_MIC.toString())); assertThat(properties.getProperty(STRICT_HOST_KEY_CHECKING), equalTo("ask")); }