Package com.jcraft.jsch

Examples of com.jcraft.jsch.Channel


  ChannelSftp newSftp() throws TransportException {
    final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
    try {
      // @TODO: Fix so that this operation is generic and casting to
      // JschSession is no longer necessary.
      final Channel channel = ((JschSession) getSession())
          .getSftpChannel();
      channel.connect(tms);
      return (ChannelSftp) channel;
    } catch (JSchException je) {
      throw new TransportException(uri, je.getMessage(), je);
    }
  }
View Full Code Here


      throws OpenShiftSSHOperationException {
    final Session session = getSSHSession();
    if (session == null) {
      throw new OpenShiftSSHOperationException("No SSH session available for application ''{0}''", this.getName());
    }
    Channel channel = null;
    BufferedReader reader = null;
    try {
      session.openChannel("exec");
      channel = session.openChannel("exec");
      ((ChannelExec) channel).setCommand(command);
      channel.connect();
      return sshStream.getLines(channel);
    } catch (JSchException e) {
      throw new OpenShiftSSHOperationException(e, "Failed to list forwardable ports for application \"{0}\"",
          this.getName());
    } catch (IOException e) {
      throw new OpenShiftSSHOperationException(e, "Failed to list forwardable ports for application \"{0}\"",
          this.getName());
    } finally {

      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          LOGGER.error("Failed to close SSH error stream reader", e);
        }
      }

      if (channel != null && channel.isConnected()) {
        channel.disconnect();
      }
    }
  }
View Full Code Here

                session.setPassword(password);
            }

            session.connect();

            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            // Direct stderr output of command
            InputStream err = ((ChannelExec) channel).getErrStream();
            InputStreamReader errStrRdr = new InputStreamReader(err, "UTF-8");
            Reader errStrBufRdr = new BufferedReader(errStrRdr);

            // Direct stdout output of command
            InputStream out = channel.getInputStream();
            InputStreamReader outStrRdr = new InputStreamReader(out, "UTF-8");
            Reader outStrBufRdr = new BufferedReader(outStrRdr);

            StringBuffer stdout = new StringBuffer();
            StringBuffer stderr = new StringBuffer();

            channel.connect(5000)// timeout after 5 seconds
            while (true) {
                if (channel.isClosed()) {
                    break;
                }

                // Read from both streams here so that they are not blocked,
                // if they are blocked because the buffer is full, channel.isClosed() will never
                // be true.
                int ch;
                while (outStrBufRdr.ready() && (ch = outStrBufRdr.read()) > -1) {
                    stdout.append((char) ch);
                }
                while (errStrBufRdr.ready() && (ch = errStrBufRdr.read()) > -1) {
                    stderr.append((char) ch);
                }

                try {
                    Thread.sleep(100);
                } catch (InterruptedException ie) {
                }
            }

            // In case there's still some more stuff in the buffers, read them
            int ch;
            while ((ch = outStrBufRdr.read()) > -1) {
                stdout.append((char) ch);
            }
            while ((ch = errStrBufRdr.read()) > -1) {
                stderr.append((char) ch);
            }

            // After the command is executed, gather the results (both stdin and stderr).
            result.append(stdout.toString());
            result.append(stderr.toString());

            // Shutdown the connection
            channel.disconnect();
            session.disconnect();
        }
        catch(Throwable e){
            e.printStackTrace();
            // Return empty string if we can't connect.
View Full Code Here

            session.setConfig(config);

            session.connect(5000); // timeout after 5 seconds

            // exec 'scp -t rfile' remotely
            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command);

            // get I/O streams for remote scp
            OutputStream out=channel.getOutputStream();
            InputStream in=channel.getInputStream();

            channel.connect();

            if(checkAck(in)!=0){
                return false;
            }

            File _lfile = new File(local_file);

            if(ptimestamp){
                command="T "+(_lfile.lastModified()/1000)+" 0";
                // The access time should be sent here,
                // but it is not accessible with JavaAPI ;-<
                command+=(" "+(_lfile.lastModified()/1000)+" 0\n");
                out.write(command.getBytes()); out.flush();
                if(checkAck(in)!=0){
                    return false;
                }
            }

            // send "C0644 filesize filename", where filename should not include '/'
            long filesize=_lfile.length();
            command="C0644 "+filesize+" ";
            if(local_file.lastIndexOf('/')>0){
                command+=local_file.substring(local_file.lastIndexOf('/')+1);
            }
            else{
                command+=local_file;
            }
            command+="\n";
            out.write(command.getBytes()); out.flush();
            if(checkAck(in)!=0){
                return false;
            }

            // send a content of lfile
            fis=new FileInputStream(local_file);
            byte[] buf=new byte[1024];
            while(true){
                int len=fis.read(buf, 0, buf.length);
                if(len<=0) break;
                out.write(buf, 0, len); //out.flush();
            }
            fis.close();
            fis=null;
            // send '\0'
            buf[0]=0; out.write(buf, 0, 1); out.flush();
            if(checkAck(in)!=0){
                return false;
            }
            out.close();

            channel.disconnect();
            session.disconnect();
        }
        catch(Exception e){
            System.out.println(e);
            try{if(fis!=null)fis.close();}catch(Exception ee){}
View Full Code Here

            session.setConfig(config);

            session.connect();

            // exec 'scp -f rfile' remotely
            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command);

            // get I/O streams for remote scp
            OutputStream out=channel.getOutputStream();
            InputStream in=channel.getInputStream();

            channel.connect();
            byte[] buf=new byte[1024];

            // send '\0'
            buf[0]=0; out.write(buf, 0, 1); out.flush();
View Full Code Here

      throws OpenShiftSSHOperationException {
    final Session session = getSSHSession();
    if (session == null) {
      throw new OpenShiftSSHOperationException("No SSH session available for application ''{0}''", this.getName());
    }
    Channel channel = null;
    BufferedReader reader = null;
    try {
      session.openChannel("exec");
      channel = session.openChannel("exec");
      ((ChannelExec) channel).setCommand(command);
      channel.connect();
      return sshStream.getLines(channel);
    } catch (JSchException e) {
      throw new OpenShiftSSHOperationException(e, "Failed to execute remote ssh command \"{0}\"",
          this.getName());
    } catch (IOException e) {
      throw new OpenShiftSSHOperationException(e, "Failed to execute remote ssh command \"{0}\"",
          this.getName());
    } finally {

      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          LOGGER.error("Failed to close SSH error stream reader", e);
        }
      }

      if (channel != null && channel.isConnected()) {
        channel.disconnect();
      }
    }
  }
View Full Code Here

      // session.setConfig("StrictHostKeyChecking", "no");

      // session.connect();
      session.connect(30000); // making a connection with timeout.

      Channel channel = session.openChannel("shell");

      // Enable agent-forwarding.
      // ((ChannelShell)channel).setAgentForwarding(true);

      channel.setInputStream(System.in);
      /*
       * // a hack for MS-DOS prompt on Windows. channel.setInputStream(new FilterInputStream(System.in){ public int read(byte[] b, int off, int
       * len)throws IOException{ return in.read(b, off, (len>1024?1024:len)); } });
       */

      channel.setOutputStream(System.out);

      /*
       * // Choose the pty-type "vt102". ((ChannelShell)channel).setPtyType("vt102");
       */

      /*
       * // Set environment variable "LANG" as "ja_JP.eucJP". ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
       */

      // channel.connect();
      channel.connect(3 * 1000);
  } catch (Exception e) {
      System.out.println(e);
  }
    }
View Full Code Here

     * 返回执行命令的内容
     * @param command
     * @return String
     */
    public  String execStr(String command) {   
    Channel channel=getChannel()
    String res="";
    try {
      ((ChannelExec)channel).setCommand(command);
      InputStream in=channel.getInputStream();
      channel.setInputStream(null);
      ((ChannelExec)channel).setErrStream(System.err);
       channel.connect();       
       res=FileUtil.readInputStreamToString(in, "UTF-8");
       in=null;
//       log.info("命令:"+command+"执行完毕\n");
//       log.info("命令执行结果为:\n"+res);
    } catch (IOException e) {
View Full Code Here

    this.in = in;
  }

  public CommandChannel build() {
    try {
      Channel channel = session.openChannel("exec");
      ((ChannelExec) channel).setCommand(command);
      channel.setInputStream(in);
      return new CommandChannelSuccess(channel);

    } catch (JSchException e) {
      return new CommandChannelFailed(e);
View Full Code Here

      throws OpenShiftSSHOperationException {
    final Session session = getSSHSession();
    if (session == null) {
      throw new OpenShiftSSHOperationException("No SSH session available for application ''{0}''", this.getName());
    }
    Channel channel = null;
    BufferedReader reader = null;
    try {
      session.openChannel("exec");
      channel = session.openChannel("exec");
      ((ChannelExec) channel).setCommand(command);
      channel.connect();
      return sshStream.getLines(channel);
    } catch (JSchException e) {
      throw new OpenShiftSSHOperationException(e, "Failed to execute remote ssh command \"{0}\"",
          this.getName());
    } catch (IOException e) {
      throw new OpenShiftSSHOperationException(e, "Failed to execute remote ssh command \"{0}\"",
          this.getName());
    } finally {

      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          LOGGER.error("Failed to close SSH error stream reader", e);
        }
      }

      if (channel != null && channel.isConnected()) {
        channel.disconnect();
      }
    }
  }
View Full Code Here

TOP

Related Classes of com.jcraft.jsch.Channel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.