Package com.jcraft.jsch

Examples of com.jcraft.jsch.Channel


      session.connect();

      String command=JOptionPane.showInputDialog("Enter command",
                                                 "set|grep SSH");

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

      // X Forwarding
      // channel.setXForwarding(true);

      //channel.setInputStream(System.in);
      channel.setInputStream(null);

      //channel.setOutputStream(System.out);

      //FileOutputStream fos=new FileOutputStream("/tmp/stderr");
      //((ChannelExec)channel).setErrStream(fos);
      ((ChannelExec)channel).setErrStream(System.err);

      InputStream in=channel.getInputStream();

      channel.connect();

      byte[] tmp=new byte[1024];
      while(true){
        while(in.available()>0){
          int i=in.read(tmp, 0, 1024);
          if(i<0)break;
          logger.info(new String(tmp, 0, i));
        }
        if(channel.isClosed()){
          logger.info("exit-status: "+channel.getExitStatus());
          break;
        }
        try{Thread.sleep(1000);}catch(Exception ee){
//          do nothing
        }
      }
      channel.disconnect();
      session.disconnect();
    }
    catch(Exception e){
      logger.info(e);
    }
View Full Code Here


    final JSch jsch = new JSch();
    final Profile.SSH x = box.getProfile().getSSH();
    final Session session = jsch.getSession(x.getAuth().getUsername(), x.getHostname(), x.getPort());
    session.setUserInfo(new PresetUserInfo(x.getAuth().getPassword(), null));
    session.connect(30000);
    final Channel channel=session.openChannel("shell");
    channel.setInputStream(System.in);
    channel.setOutputStream(System.out);
    channel.connect(3000);
    try {
      while (channel.isConnected()) {
        Thread.sleep(250); // bit rubbish way of doing this
      }
    } finally {
      channel.disconnect();
    }
    return null;
  }
View Full Code Here

            String command = execCommand.toString();
            LOGGER.info("Exec " + command);

            // Open an execution channel that supports SSH agent forwarding
            Channel channel = sess.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);
            channel.connect();

            // Wait for the channel to close
            while (true) {
                if (channel.isClosed()) {
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                }
            }
            int result = channel.getExitStatus();
            if (result != 0) {
                LOGGER.warning("Download of slave.jar failed.  Return code = " + result);
                throw new IOException(
                        "Download of slave.jar failed.  Return code = "
                                + result);
            }
            channel.disconnect();

            // Execute the slave.jar to establish a connection
            // Make sure to enable SSH agent forwarding
            logger.println("Executing slave jar to make connection...");
            final Channel slaveChannel = sess.openChannel("exec");
            String sshWrapperPath = "/usr/libexec/openshift/cartridges/jenkins/bin/git_ssh_wrapper.sh";
            ((ChannelExec) slaveChannel).setEnv("GIT_SSH", sshWrapperPath);
            ((ChannelExec) slaveChannel).setAgentForwarding(true);
            String jarCachePath=System.getenv("JENKINS_JAR_CACHE_PATH");
            if (jarCachePath==null) {
                jarCachePath="$OPENSHIFT_DATA_DIR/.jenkins/cache/jars";
            }
            //jar-cache parameter needed for jenkins 1.540+
            ((ChannelExec) slaveChannel)
            .setCommand("java -jar $OPENSHIFT_DATA_DIR/jenkins/slave.jar -jar-cache "+jarCachePath);
            InputStream serverOutput = slaveChannel.getInputStream();
            OutputStream clientInput = slaveChannel.getOutputStream();
            slaveChannel.connect();
            if (slaveChannel.isClosed()) {
                LOGGER.severe("Slave connection terminated early with exit = "
                        + channel.getExitStatus());
            }

            computer.setChannel(serverOutput, clientInput, taskListener,
                    new Listener() {

                        public void onClosed(hudson.remoting.Channel channel,
                                             IOException cause) {
                            slaveChannel.disconnect();
                            sess.disconnect();
                        }
                    });

            LOGGER.info("Slave connected.");
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

        log("done.\n");
    }

    private void doSingleTransfer() throws IOException, JSchException {
        String cmd = "scp -t " + remotePath;
        Channel channel = openExecChannel(cmd);
        try {

            OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            channel.connect();

            waitForAck(in);
            sendFileToRemote(localFile, in, out);
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
    }
View Full Code Here

            }
        }
    }

    private void doMultipleTransfer() throws IOException, JSchException {
        Channel channel = openExecChannel("scp -r -d -t " + remotePath);
        try {
            OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            channel.connect();

            waitForAck(in);
            for (Iterator i = directoryList.iterator(); i.hasNext();) {
                Directory current = (Directory) i.next();
                sendDirectory(current, in, out);
            }
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
    }
View Full Code Here

        String command = "scp -f ";
        if (isRecursive) {
            command += "-r ";
        }
        command += remoteFile;
        Channel channel = openExecChannel(command);
        try {
            // get I/O streams for remote scp
            OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            channel.connect();

            sendAck(out);
            startRemoteCpProtocol(in, out, localFile);
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
        log("done\n");
    }
View Full Code Here

                session.setPassword(userCreds[1]);
            }

            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp c = (ChannelSftp) channel;

            File downloadFile = new File(path);
            FileOutputStream tempFileOutputStream =
                    new FileOutputStream(downloadFile);

            IOUtils.copy(c.get(filePath), tempFileOutputStream);
            channel.disconnect();
            session.disconnect();

            return downloadFile;
        } catch (Exception e) {
            final String msg = "Error downloading namespace";
View Full Code Here

            session.setConfig(hash);
            session.setPort(port);
            session.setPassword(password);
            session.connect();

            Channel channel = session.openChannel(CHANNEL_SFTP);
            channel.connect();

            channelSftp = (ChannelSftp) channel;
            setHome(channelSftp.pwd());
        }
        catch (JSchException e)
View Full Code Here

            session = jsch.getSession(user, host);
            session.setConfig(hash);
            session.setPort(port);
            session.connect();

            Channel channel = session.openChannel(CHANNEL_SFTP);
            channel.connect();

            channelSftp = (ChannelSftp) channel;
            setHome(channelSftp.pwd());
        }
        catch (JSchException e)
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.