com.jcraft.jsch.Channel.isConnected()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(311)

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

Channel.isConnected介绍

[英]returns true if this channel is currently connected (which also means that the session is still connected).
[中]如果此通道当前已连接(这也意味着会话仍处于连接状态),则返回true。

代码示例

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

/**
 * 关闭会话通道
 * 
 * @param channel 会话通道
 * @since 4.0.3
 */
public static void close(Channel channel) {
  if (channel != null && channel.isConnected()) {
    channel.disconnect();
  }
}

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

/**
 * 关闭会话通道
 * 
 * @param channel 会话通道
 * @since 4.0.3
 */
public static void close(Channel channel) {
  if (channel != null && channel.isConnected()) {
    channel.disconnect();
  }
}

代码示例来源: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: dufyun/learn-tech-collection

@Override
public boolean isConnected() {
  boolean flag = false;
  if(this.channel != null && this.channel.isConnected()){
    flag = true;
  }
  return flag;
}

代码示例来源:origin: cn.hutool/hutool-all

/**
 * 关闭会话通道
 * 
 * @param channel 会话通道
 * @since 4.0.3
 */
public static void close(Channel channel) {
  if (channel != null && channel.isConnected()) {
    channel.disconnect();
  }
}

代码示例来源:origin: Impetus/jumbune

public void closeChannel(Channel channel){
  if(channel!=null && channel.isConnected()){
    channel.disconnect();
  }
}

代码示例来源:origin: org.hudsonci.plugins/gerrit-events

/**
  * 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();
}

代码示例来源:origin: com.sonyericsson.hudson.plugins.gerrit/gerrit-events

/**
 * 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();
}

代码示例来源:origin: Impetus/jumbune

public void closeChannel(Channel channel){
  if(channel!=null && channel.isConnected()){
    channel.disconnect();
  }
}

代码示例来源:origin: Impetus/jumbune

public void closeChannel(Channel channel){
  if(channel!=null && channel.isConnected()){
    channel.disconnect();
  }
}

代码示例来源:origin: Impetus/jumbune

public void closeChannel(Channel channel){
  if(channel!=null && channel.isConnected()){
    channel.disconnect();
  }
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

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

代码示例来源:origin: bradand/XMouse

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> {

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.jsch

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

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

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

代码示例来源:origin: org.ikasan/ikasan-connector-sftp-classes

/**
 * 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);
}

代码示例来源:origin: com.jcraft.jsch/com.springsource.com.jcraft.jsch

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

代码示例来源:origin: net.sourceforge.expectj/expectj

/**
 * 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();
}

代码示例来源:origin: com.redhat.rhevm.api/rhevm-api-powershell-expectj

/**
 * 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();
}

代码示例来源:origin: com.axway.ats.expectj/ats-expectj

/**
 * 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();
}

相关文章