com.jcraft.jsch.JSch.setKnownHosts()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(266)

本文整理了Java中com.jcraft.jsch.JSch.setKnownHosts()方法的一些代码示例,展示了JSch.setKnownHosts()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSch.setKnownHosts()方法的具体详情如下:
包路径:com.jcraft.jsch.JSch
类名称:JSch
方法名:setKnownHosts

JSch.setKnownHosts介绍

[英]Creates a Host key repository from an InputStream. This method uses the same format as OpenSSH's known_hosts file (I hope).

This has no effect if #setHostKeyRepository was already called with an object which is not of class KnownHosts.
[中]从InputStream创建主机密钥存储库。此方法使用与OpenSSH已知的_hosts文件相同的格式(我希望如此)。
如果已使用非KnownHosts类的对象调用了#setHostKeyRepository,则此操作无效。

代码示例

代码示例来源:origin: apache/incubator-gobblin

@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;
 }
};

代码示例来源:origin: apache/nifi

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;
}

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: stackoverflow.com

jsch.setKnownHosts( knownHostsFilename );

代码示例来源:origin: spring-cloud/spring-cloud-config

@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);
}

代码示例来源:origin: apache/incubator-gobblin

this.session.setConfig("StrictHostKeyChecking", "no");
} else {
 jsch.setKnownHosts(knownHosts);

代码示例来源:origin: apache/nifi

jsch.setKnownHosts(hostKeyVal);

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

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.
  }
}

代码示例来源:origin: spring-projects/spring-integration

this.jsch.setKnownHosts(this.knownHosts);

代码示例来源:origin: ioFog/Agent

/**
 * adds server rsa key to known hosts
 */
public void setKnownHost() throws JSchException {
  jschSSHChannel.setKnownHosts(new ByteArrayInputStream(this.rsaKey.getBytes(UTF_8)));
}

代码示例来源:origin: stackoverflow.com

JSch jsch = new JSch();
<br>
jsch.setKnownHosts(myKonfig.getKnownHostsFile());
<br>
 String privKeyFile = myKonfig.getPrivateKeyFile();
 <br>
 jsch.addIdentity(privKeyFile);
 <br>
 </br>
 </br>
</br>
</br>

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: com.g2forge.gearbox/gb-git

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
  }
}

代码示例来源:origin: stackoverflow.com

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);

代码示例来源:origin: tmobile/pacbot

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;
}

代码示例来源:origin: org.kuali.common/kuali-util

protected JSch getJSch() throws JSchException {
  JSch jsch = getJSch(privateKeys, privateKeyStrings);
  if (strictHostKeyChecking && knownHosts.exists()) {
    String path = LocationUtils.getCanonicalPath(knownHosts);
    jsch.setKnownHosts(path);
  }
  return jsch;
}

代码示例来源:origin: org.kuali.common/kuali-util

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;
}

代码示例来源:origin: Skarafaz/mercury

@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;
}

代码示例来源:origin: com.meltmedia.cadmium/cadmium-core

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;
 }

代码示例来源:origin: org.mule.connectors/mule-ftp-connector

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"));
}

相关文章