com.jcraft.jsch.JSch类的使用及代码示例

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

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

JSch介绍

[英]This class serves as a central configuration point, and as a factory for Session objects configured with these settings.

  • Use #getSession to start a new Session.
  • Use one of the #addIdentity methods for public-key authentication.
  • Use #setKnownHosts to enable checking of host keys.
  • See #setConfig(String,String) for a list of configuration options.
    [中]此类用作中心配置点,并用作使用这些设置配置的会话对象的工厂。
    *使用#getSession启动新会话。
    *使用#addIdentity方法之一进行公钥身份验证。
    *使用#setKnownHosts启用主机密钥检查。
    *有关配置选项的列表,请参见#setConfig(String,String)。

代码示例

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

System.out.println("preparing the host information for sftp.");
try {
  JSch jsch = new JSch();
  session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
  session.setPassword(SFTPPASS);
  java.util.Properties config = new java.util.Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);
  session.connect();
  System.out.println("Host connected.");
  channel = session.openChannel("sftp");
  System.out.println("sftp channel opened and connected.");
  channelSftp = (ChannelSftp) channel;
  channelSftp.cd(SFTPWORKINGDIR);
  File f = new File(fileName);
  channelSftp.put(new FileInputStream(f), f.getName());
  log.info("File transfered successfully to host.");
} catch (Exception ex) {
  channelSftp.exit();
  System.out.println("sftp Channel exited.");
  channel.disconnect();

代码示例来源:origin: org.apache.hadoop/hadoop-common

if (channel.isConnected()) {
  return channel;
 } else {
JSch jsch = new JSch();
Session session = null;
try {
  jsch.addIdentity(keyFile);
  session = jsch.getSession(user, host);
 } else {
  session = jsch.getSession(user, host, port);
 session.setPassword(password);
 java.util.Properties config = new java.util.Properties();
 config.put("StrictHostKeyChecking", "no");
 session.setConfig(config);
 session.connect();
 channel = (ChannelSftp) session.openChannel("sftp");
 channel.connect();

代码示例来源: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: apache/kylin

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

代码示例来源:origin: org.apache.hadoop/hadoop-common

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

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

JSch jsch = new JSch();
jsch.setKnownHosts( knownHostsFilename );
Session session = jsch.getSession( "remote-username", "remote-host" );    
 session.setUserInfo(ui);
 session.setPassword( "remote-password" );
session.connect();
Channel channel = session.openChannel( "sftp" );
sftpChannel.get("remote-file", "local-file" );
InputStream in = sftpChannel.get( "remote-file" );
sftpChannel.exit();
session.disconnect();

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

...
 java.util.Properties config = new java.util.Properties();
 config.put("StrictHostKeyChecking", "no");
 JSch ssh = new JSch();
 session = ssh.getSession(sshSolrUsername, sshSolrHost, 22);
 session.setConfig(config);
 session.setPassword(sshSolrPassword);
 session.connect();
 channel = session.openChannel("exec");        
 ((ChannelExec)channel ).setCommand("cd " + sourcePath);     
 exec.setInputStream(null);
 ((ChannelExec)channel ).setErrStream(System.err);
 InputStream in = channel .getInputStream();
 channel .connect();
 int status = checkStatus(channel , in);
 channel.disconnect();
 ...

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

JSch jsch = new JSch();

java.util.Properties configuration = new java.util.Properties();
configuration.put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256");
configuration.put("StrictHostKeyChecking", "no");

Session session = jsch.getSession("username", "hostname", 22);
session.setPassword("password");
session.setConfig(configuration);
session.connect();

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

String host = "XXXXXX";
String passwd = "XXXXXX";
JSch conn = new JSch();
Session session = null;
session = conn.getSession(username, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
channel.cd("/tmp/qtmp");
InputStream in = channel.get("testScp");
String lf = "OBJECT_FILE";
FileOutputStream tergetFile = new FileOutputStream(lf);
while ( (c= in.read()) != -1 ) {
  tergetFile.write(c);
in.close();
tergetFile.close();

代码示例来源:origin: alibaba/jstorm

public void execute(String command) throws Exception {
  JSch jsch = new JSch();
  Session session = jsch.getSession(username, host, 22);
  session.setPassword(password);
  session.setConfig("StrictHostKeyChecking", "no");
  session.connect();
  Channel channel = session.openChannel("exec");
    while (in.available() > 0) {
      int i = in.read(tmp, 0, 1024);
      if (i < 0) break;
      if (in.available() > 0) continue;
      if (channel.getExitStatus() != 0)
        throw new Exception("exitStatus:" + channel.getExitStatus());

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

final JSch jsch = new JSch();
try {
  final String username = ctx.getProperty(USERNAME).evaluateAttributeExpressions(flowFile).getValue();
  final Session session = jsch.getSession(username,
    ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue(),
    ctx.getProperty(PORT).evaluateAttributeExpressions(flowFile).asInteger().intValue());
        proxyHTTP.setUserPasswd(proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword());
      session.setProxy(proxyHTTP);
      break;
    case SOCKS:
        proxySOCKS5.setUserPasswd(proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword());
      session.setProxy(proxySOCKS5);
      break;
    jsch.setKnownHosts(hostKeyVal);
    jsch.addIdentity(privateKeyFile, ctx.getProperty(PRIVATE_KEY_PASSPHRASE).evaluateAttributeExpressions(flowFile).getValue());
    this.homeDir = sftp.getHome();
  } catch (SftpException e) {
  throw new IOException("Failed to obtain connection to remote host due to " + e.toString(), e);

代码示例来源:origin: simpleci/simpleci

public SshClient(String host, String user, String password) throws JSchException {
  if(!Utils.waitForPort(host, SSH_PORT, 10, 1000)) {
    throw new JSchException(String.format("Cant connect to %s:%d", host, SSH_PORT));
  }
  JSch jsch = new JSch();
  session = jsch.getSession(user, host, SSH_PORT);
  java.util.Properties config = new java.util.Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);
  session.setPassword(password);
  session.connect();
}

代码示例来源:origin: org.apache.uima/uima-ducc-common

private Session createSSHSession(String host) throws JSchException {
  Session newSession = jsch.getSession(sshUser, host, 22);
  newSession.setTimeout(500);
  Properties props = new Properties();
  props.put("StrictHostKeyChecking", "no");
  newSession.setConfig(props);
  newSession.connect();
  return newSession;
}

代码示例来源:origin: alipay/rdf-file

/**
 * 通过用户名和私钥认证,发起ssh连接。
 * 
 * @param user           FTP登录用户
 * @return
 * @throws JSchException 
 */
private static Session connectByIdentity(SFTPUserInfo user) throws JSchException {
  if(RdfFileUtil.isBlank(user.getIdentityFile())){
    throw new RdfFileException("rdf-file#JschFactory.connectByPasswd私钥文件不能为空"
        , RdfErrorEnum.ILLEGAL_ARGUMENT);
  }
  Properties sshConfig = new Properties();
  sshConfig.put("StrictHostKeyChecking", "no");
  JSch.setConfig(sshConfig);
  JSch jsch = new JSch();
  jsch.addIdentity(user.getIdentityFile());
  Session sshSession = jsch.getSession(user.getUser(), user.getHost(), user.getPort());
  return sshSession;
}

代码示例来源:origin: tote/ssh8

protected void initSessionWithKey(String user, String pathToKey, String domain, int port) throws JSchException {
  System.out.println("initializing session with key.");
  jsch.addIdentity(ToteUtils.getFilePath(pathToKey));
  session = jsch.getSession(user, domain, port);
  //session.setPassword(password);
  Properties config = new Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);
}

代码示例来源:origin: tote/ssh8

protected void initSession(String user, String password, String domain, int port) throws JSchException {
  System.out.println("initializing session without key.");
  session = jsch.getSession(user, domain, port);
  session.setPassword(password);
  Properties config = new Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);
}

代码示例来源: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: stackoverflow.com

new Thread(new Runnable() {
   @Override
   public void run() {
     Session session;
     JSch jsch;
     try {
       jsch = new JSch();
       jsch.addIdentity(privateKey, "yourprivatekey");
       session = jsch.getSession("git", "github.com", 22);
       session.setPassword("yourpass");
       // Avoid asking for key confirmation
       Properties prop = new Properties();
       prop.put("StrictHostKeyChecking", "no");
       session.setConfig(prop);
       session.connect();
       if(session.isConnected()){
         System.out.println(this.getClass().getSimpleName() + " CONNECTED");
         System.out.println(this.getClass().getSimpleName() + " YOO " + jsch.getIdentityRepository().getName()+" "+session.getClientVersion() + " " + session.isConnected());
       }else{
         System.out.println(this.getClass().getSimpleName() + " NOT CONNECTED");
       }
     }catch (Exception e){
       e.printStackTrace();
     }
   }
 }).start();

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

JSch shell = new JSch();
Properties config = new Properties();
config.put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256");
config.put("StrictHostKeyChecking", "no");

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

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
 while (in.available() > 0) {
   int i = in.read(tmp, 0, 1024);
   if (i < 0) {
     break;
session.disconnect();

相关文章