/** * 关闭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); }
/** * 关闭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); }
/** * 关闭所有SSH连接会话 */ public void closeAll() { Collection<Session> sessions = sessionPool.values(); for (Session session : sessions) { if (session.isConnected()) { session.disconnect(); } } sessionPool.clear(); } }
/** * 关闭所有SSH连接会话 */ public void closeAll() { Collection<Session> sessions = sessionPool.values(); for (Session session : sessions) { if (session.isConnected()) { session.disconnect(); } } sessionPool.clear(); } }
/** * 关闭SSH连接会话 * * @param session SSH会话 */ public static void close(Session session) { if (session != null && session.isConnected()) { session.disconnect(); } JschSessionPool.INSTANCE.remove(session); }
/** * 关闭SSH连接会话 * * @param session SSH会话 */ public static void close(Session session) { if (session != null && session.isConnected()) { session.disconnect(); } JschSessionPool.INSTANCE.remove(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; }
/** * 绑定端口到本地。 一个会话可绑定多个端口 * * @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; }
/** * 获得一个SSH跳板机会话,重用已经使用的会话 * * @param sshHost 跳板机主机 * @param sshPort 跳板机端口 * @param sshUser 跳板机用户名 * @param sshPass 跳板机密码 * @return SSH会话 */ public Session getSession(String sshHost, int sshPort, String sshUser, String sshPass) { final String key = StrUtil.format("{}@{}:{}", sshUser, sshHost, sshPort); Session session = get(key); if (null == session || false == session.isConnected()) { synchronized (lock) { session = get(key); if (null == session || false == session.isConnected()) { session = JschUtil.openSession(sshHost, sshPort, sshUser, sshPass); put(key, session); } } } return session; }
/** * 获得一个SSH跳板机会话,重用已经使用的会话 * * @param sshHost 跳板机主机 * @param sshPort 跳板机端口 * @param sshUser 跳板机用户名 * @param sshPass 跳板机密码 * @return SSH会话 */ public Session getSession(String sshHost, int sshPort, String sshUser, String sshPass) { final String key = StrUtil.format("{}@{}:{}", sshUser, sshHost, sshPort); Session session = get(key); if (null == session || false == session.isConnected()) { synchronized (lock) { session = get(key); if (null == session || false == session.isConnected()) { session = JschUtil.openSession(sshHost, sshPort, sshUser, sshPass); put(key, session); } } } return session; }
@Override public boolean isOpen() { return !this.closed && this.jschSession.isConnected(); }
@Override public boolean isConnected() { return ftp != null && sock.isConnected(); }
public boolean isConnected() { return this.session.isConnected(); }
/** {@inheritDoc} */ @Override public void disconnect() { if (sock.isConnected()) sock.disconnect(); }
if (ses != null && ses.isConnected()) ses.disconnect();
/** * {@link org.apache.commons.vfs2.provider.sftp.SftpFileSystem#getChannel() } * */ private void ensureSession() throws FileSystemException { if ( this.session == null || !this.session.isConnected() ) { this.doCloseCommunicationLink(); UserAuthenticationData authData = null; Session session; try { GenericFileName e = (GenericFileName) this.getRootName(); authData = UserAuthenticatorUtils.authenticate( this.getFileSystemOptions(), SftpFileProvider.AUTHENTICATOR_TYPES ); session = SftpClientFactory.createConnection( e.getHostName(), e.getPort(), UserAuthenticatorUtils.getData( authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar( e.getUserName() ) ), UserAuthenticatorUtils.getData( authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar( e.getPassword() ) ), this.getFileSystemOptions() ); } catch ( Exception var7 ) { throw new FileSystemException( "vfs.provider.sftp/connect.error", this.getRootName(), var7 ); } finally { UserAuthenticatorUtils.cleanup( authData ); } this.session = session; } }
@Override public void close() { this.closed = true; if (this.wrapper != null) { if (this.channel != null) { this.channel.disconnect(); } this.wrapper.close(); } else { if (this.jschSession.isConnected()) { this.jschSession.disconnect(); } } }
void connect() { try { if (!this.jschSession.isConnected()) { this.jschSession.connect(); } this.channel = (ChannelSftp) this.jschSession.openChannel("sftp"); if (this.channel != null && !this.channel.isConnected()) { this.channel.connect(); } } catch (JSchException e) { this.close(); throw new IllegalStateException("failed to connect", e); } }
@Override public void clear() { if (session != null && session.isConnected()) { session.disconnect(); session = null; } }
while (!session.isConnected()) { try { retries++;