/** * 关闭会话通道 * * @param channel 会话通道 * @since 4.0.3 */ public static void close(Channel channel) { if (channel != null && channel.isConnected()) { channel.disconnect(); } }
/** * 关闭会话通道 * * @param channel 会话通道 * @since 4.0.3 */ public static void close(Channel channel) { if (channel != null && channel.isConnected()) { channel.disconnect(); } }
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; } }
@Override public boolean isConnected() { boolean flag = false; if(this.channel != null && this.channel.isConnected()){ flag = true; } return flag; }
/** * 关闭会话通道 * * @param channel 会话通道 * @since 4.0.3 */ public static void close(Channel channel) { if (channel != null && channel.isConnected()) { channel.disconnect(); } }
public void closeChannel(Channel channel){ if(channel!=null && channel.isConnected()){ channel.disconnect(); } }
/** * Returns if there already is an open session on this connection. * * @return true if it is so. */ @Override public synchronized boolean isSessionOpen() { return currentSession != null && currentSession.isConnected() && !currentSession.isEOF(); }
/** * Returns if there already is an open session on this connection. * @return true if it is so. */ public synchronized boolean isSessionOpen() { return currentSession != null && currentSession.isConnected() && !currentSession.isEOF(); }
public void closeChannel(Channel channel){ if(channel!=null && channel.isConnected()){ channel.disconnect(); } }
public void closeChannel(Channel channel){ if(channel!=null && channel.isConnected()){ channel.disconnect(); } }
public void closeChannel(Channel channel){ if(channel!=null && channel.isConnected()){ channel.disconnect(); } }
@Override public void close() throws IOException { if (currentStream != null) { currentStream.close(); } if (channel != null && channel.isConnected()) { channel.disconnect(); channel = null; } if (session != null) { session.disconnect(); session = null; } }
public boolean executeShellCommand(String cmd){ MainActivity.recentCmdTextView.setText(cmd); if(session==null){ return false; } if(session.isConnected() && channel.isConnected()){ try { //Log.d(TAG,cmd+", "+session.isConnected()); cmd=cmd+"\r\n"; pin.write(cmd.getBytes()); pin.flush(); return true; } catch (IOException e) { e.printStackTrace(); } } return false; } public class SshExecTask extends AsyncTask<String, String, String> {
void write(Packet packet) throws Exception{ if(reply){ channel.reply=-1; } session.write(packet); if(reply){ long start=System.currentTimeMillis(); long timeout=channel.connectTimeout; while(channel.isConnected() && channel.reply==-1){ try{Thread.sleep(10);} catch(Exception ee){ } if(timeout>0L && (System.currentTimeMillis()-start)>timeout){ channel.reply=0; throw new JSchException("channel request: timeout"); } } if(channel.reply==0){ throw new JSchException("failed to send channel request"); } } } }
void write(Packet packet) throws Exception{ if(reply){ channel.reply=-1; } session.write(packet); if(reply){ long start=System.currentTimeMillis(); long timeout=channel.connectTimeout; while(channel.isConnected() && channel.reply==-1){ try{Thread.sleep(10);} catch(Exception ee){ } if(timeout>0L && (System.currentTimeMillis()-start)>timeout){ channel.reply=0; throw new JSchException("channel request: timeout"); } } if(channel.reply==0){ throw new JSchException("failed to send channel request"); } } } }
/** * Method that tests if the underlying library's session and channels are * valid and connected. * * @return <code>true</code> if fully connected, <code>false</code> * otherwise */ public boolean isConnected() { // Getting the status of each connection related object boolean a = (this.session == null) ? false : this.session.isConnected(); boolean b = (this.channel == null) ? false : this.channel.isConnected(); boolean c = (this.channelSftp == null) ? false : this.channelSftp.isConnected(); // If all the above are true, then we are connected // Else, two possibilities, either we are all happily disconnected, or // one of the above is null (when it shouldn't - hence assume // disconnected return (a && b && c); }
void write(Packet packet) throws Exception{ if(reply){ channel.reply=-1; } session.write(packet); if(reply){ long start=System.currentTimeMillis(); long timeout=channel.connectTimeout; while(channel.isConnected() && channel.reply==-1){ try{Thread.sleep(10);} catch(Exception ee){ } if(timeout>0L && (System.currentTimeMillis()-start)>timeout){ channel.reply=0; throw new JSchException("channel request: timeout"); } } if(channel.reply==0){ throw new JSchException("failed to send channel request"); } } } }
/** * Takes control over an existing SSH channel. * * @param channel The channel we should control. If this channel isn't * already connected, {@link Channel#connect()} will be called. * * @throws IOException If connecting the channel fails. */ public SshSpawn(Channel channel) throws IOException { if (!channel.isConnected()) { try { channel.connect(); } catch (JSchException e) { throw new IOException("Failed connecting the channel", e) ; } } this.m_channel = channel; m_toSocket = m_channel.getInputStream(); m_fromSocket = m_channel.getOutputStream(); }
/** * Takes control over an existing SSH channel. * * @param channel The channel we should control. If this channel isn't * already connected, {@link Channel#connect()} will be called. * * @throws IOException If connecting the channel fails. */ public SshSpawn(Channel channel) throws IOException { if (!channel.isConnected()) { try { channel.connect(); } catch (JSchException e) { throw new IOException("Failed connecting the channel", e) ; } } this.m_channel = channel; m_toSocket = m_channel.getInputStream(); m_fromSocket = m_channel.getOutputStream(); }
/** * Takes control over an existing SSH channel. * * @param channel The channel we should control. If this channel isn't * already connected, {@link Channel#connect()} will be called. * * @throws IOException If connecting the channel fails. */ public SshSpawn( Channel channel ) throws IOException { if( !channel.isConnected() ) { try { channel.connect(); } catch( JSchException e ) { throw new IOException( "Failed connecting the channel", e ); } } this.m_channel = channel; m_toSocket = m_channel.getInputStream(); m_fromSocket = m_channel.getOutputStream(); }