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

x33g5p2x  于2022-01-29 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(1346)

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

Session介绍

[英]A Session represents a connection to a SSH server. One session can contain multiple Channels of various types, created with #openChannel.

A session is opened with #connect() and closed with #disconnect.

The fact that a Session implements Runnable is an implementation detail.
[中]会话表示到SSH服务器的连接。一个会话可以包含使用#openChannel创建的多种类型的频道。
会话用#connect()打开,用#disconnect()关闭。
会话实现Runnable是一个实现细节。

代码示例

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

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
session.disconnect();

代码示例来源:origin: looly/hutool

/**
 * 关闭SSH连接会话
 * 
 * @param key 主机,格式为user@host:port
 */
public void close(String key) {
  Session session = sessionPool.get(key);
  if (session != null && session.isConnected()) {
    session.disconnect();
  }
  sessionPool.remove(key);
}

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

/**
 * 绑定端口到本地。 一个会话可绑定多个端口
 * 
 * @param session 需要绑定端口的SSH会话
 * @param remoteHost 远程主机
 * @param remotePort 远程端口
 * @param localPort 本地端口
 * @return 成功与否
 * @throws JschRuntimeException 端口绑定失败异常
 */
public static boolean bindPort(Session session, String remoteHost, int remotePort, int localPort) throws JschRuntimeException {
  if (session != null && session.isConnected()) {
    try {
      session.setPortForwardingL(localPort, remoteHost, remotePort);
    } catch (JSchException e) {
      throw new JschRuntimeException(e, "From [{}] mapping to [{}] error!", remoteHost, localPort);
    }
    return true;
  }
  return false;
}

代码示例来源:origin: dboissier/mongo4idea

private Session createSshSession(SshTunnelingConfiguration sshTunnelingConfiguration,
                 ServerConfiguration.HostAndPort hostAndPort, int localPort) {
  try {
    JSch jsch = new JSch();
    String proxyUser = sshTunnelingConfiguration.getProxyUser();
    String password = sshTunnelingConfiguration.getProxyPassword();
    Session session = jsch.getSession(proxyUser, proxyHost);
    if (AuthenticationMethod.PRIVATE_KEY.equals(authenticationMethod)) {
      jsch.addIdentity(sshTunnelingConfiguration.getPrivateKeyPath(),
          sshTunnelingConfiguration.getProxyPassword());
    } else {
      session.setPassword(password);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    session.setPortForwardingL(localPort, remoteMongoHost, remoteMongoPort);

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

public void login( String password ) throws KettleJobException {
 this.password = password;
 s.setPassword( this.getPassword() );
 try {
  java.util.Properties config = new java.util.Properties();
  config.put( "StrictHostKeyChecking", "no" );
  // set compression property
  // zlib, none
  String compress = getCompression();
  if ( compress != null ) {
   config.put( COMPRESSION_S2C, compress );
   config.put( COMPRESSION_C2S, compress );
  }
  s.setConfig( config );
  s.connect();
  Channel channel = s.openChannel( "sftp" );
  channel.connect();
  c = (ChannelSftp) channel;
 } catch ( JSchException e ) {
  throw new KettleJobException( e );
 }
}

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

protected ChannelSftp getChannel(final FlowFile flowFile) throws IOException {
  if (sftp != null) {
    String sessionhost = session.getHost();
    String desthost = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue();
    if (sessionhost.equals(desthost)) {
          proxyHTTP.setUserPasswd(proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword());
        session.setProxy(proxyHTTP);
        break;
      case SOCKS:
        session.setProxy(proxySOCKS5);
        break;
    session.setConfig(properties);
      session.setPassword(password);
    session.setTimeout(connectionTimeoutMillis);
    session.connect();
    this.session = session;
    this.closed = false;
    sftp = (ChannelSftp) session.openChannel("sftp");
    sftp.connect(connectionTimeoutMillis);
    session.setTimeout(ctx.getProperty(FileTransfer.DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    if (!ctx.getProperty(USE_KEEPALIVE_ON_TIMEOUT).asBoolean()) {
      session.setServerAliveCountMax(0); // do not send keepalive message on SocketTimeoutException

代码示例来源:origin: dadoonet/fscrawler

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

代码示例来源: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: 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: org.rundeck/rundeck-core

public static void configureSession(Map<String, String> config, Session session) {
  Properties newconf = new Properties();
  newconf.putAll(config);
  session.setConfig(newconf);
  try {
    configureSessionServerAliveInterval(config, session);
    configureSessionServerAliveCountMax(config, session);
  } catch (JSchException e) {
    e.printStackTrace();
  }
}

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

private Expect4j SSH(String hostname, String username,String password, int port) throws Exception {
  JSch jsch = new JSch();
  Session session = jsch.getSession(username, hostname, port);
  if (password != null) {         
    session.setPassword(password);
  }
  Hashtable<String,String> config = new Hashtable<String,String>();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);
  session.connect(60000);
  channel = (ChannelShell) session.openChannel("shell");
  Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
  channel.connect();      
  return expect;
}

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

...
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
Hashtable<String, String> config = new Hashtable<String, String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
...

代码示例来源: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"));
}

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

public static Session createSession(JSch jsch, String username,
  String hostname, int port) throws JSchException{
 Session session=jsch.getSession(username, hostname, port);
 setProxy(session);
 Hashtable config=new Hashtable();
 config.put("PreferredAuthentications", //$NON-NLS-1$ 
   "gssapi-with-mic,publickey,password,keyboard-interactive"); //$NON-NLS-1$ 
 session.setConfig(config);
 return session;
}

相关文章

微信公众号

最新文章

更多