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();
/** * 关闭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); }
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; }
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; }
/** * 绑定端口到本地。 一个会话可绑定多个端口 * * @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; }
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);
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();
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 ); } }
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();
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
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; } }
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(); }
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; }
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); }
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();
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(); } }
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; }
... 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(); ...
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")); }
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; }