Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelExec


        c.disconnect();
        return new String(buffer);
    }

    protected String readDir(String path) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        c.setCommand("scp -r -f " + path);
        c.connect();
        String header = readLine(is);
        assertTrue(header.startsWith("D0755 0 "));
        os.write(0);
        os.flush();
        header = readLine(is);
        assertEquals("C0644 11 out.txt", header);
        int length = Integer.parseInt(header.substring(6, header.indexOf(' ', 6)));
        os.write(0);
        os.flush();
        byte[] buffer = new byte[length];
        length = is.read(buffer, 0, buffer.length);
        assertEquals(length, buffer.length);
        assertEquals(0, is.read());
        os.write(0);
        os.flush();
        header = readLine(is);
        assertEquals("E", header);

        c.disconnect();
        return new String(buffer);
    }
View Full Code Here


        c.disconnect();
        return new String(buffer);
    }

    protected String readFileError(String path) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        c.setCommand("scp -f " + path);
        c.connect();
        assertEquals(2, is.read());
        c.disconnect();
        return null;
    }
View Full Code Here

        c.disconnect();
        return null;
    }

    protected void sendFile(String path, String name, String data) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        c.setCommand("scp -t " + path);
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        c.connect();
        assertEquals(0, is.read());
        os.write(("C7777 "+ data.length() + " " + name + "\n").getBytes());
        os.flush();
        assertEquals(0, is.read());
        os.write(data.getBytes());
        os.flush();
        assertEquals(0, is.read());
        os.write(0);
        os.flush();
        Thread.sleep(100);
        c.disconnect();
    }
View Full Code Here

        Thread.sleep(100);
        c.disconnect();
    }

    protected void sendFileError(String path, String name, String data) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        c.setCommand("scp -t " + path);
        c.connect();
        assertEquals(0, is.read());
        os.write(("C7777 "+ data.length() + " " + name + "\n").getBytes());
        os.flush();
        assertEquals(2, is.read());
        c.disconnect();
    }
View Full Code Here

        assertEquals(2, is.read());
        c.disconnect();
    }

    protected void sendDir(String path, String dirName, String fileName, String data) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        c.setCommand("scp -t -r " + path);
        c.connect();
        assertEquals(0, is.read());
        os.write(("D0755 0 " + dirName + "\n").getBytes());
        os.flush();
        assertEquals(0, is.read());
        os.write(("C7777 " + data.length() + " " + fileName + "\n").getBytes());
View Full Code Here

        @Override
        public int execute(String command, OutputStream stdout, OutputStream stderr) throws IOException {
            Session session = getSession();

            ChannelExec channel;
            try {
                channel = (ChannelExec) session.openChannel("exec");

                channel.setInputStream(null);
                channel.setOutputStream(stdout);
                channel.setErrStream(stderr);

                channel.setCommand(command);

                channelCount++;
                channel.connect();
            } catch (JSchException e) {
                // TODO: Close session if it's failed??
                channelCount--;
                throw new IOException("Error opening direct-tcpip channel", e);
            }

            try {
                while (true) {
                    int exitStatus = channel.getExitStatus();
                    if (exitStatus != -1) {
                        return exitStatus;
                    }

                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        throw new IOException("Interrupted while waiting for SSH command execution", e);
                    }
                }
            } finally {
                channel.disconnect();
                channelCount--;
            }
        }
View Full Code Here

    final Session session = getSession();

    session.connect();

    final ChannelExec channel = (ChannelExec) session.openChannel("exec");

    channel.setCommand(command);

    channel.connect();

    //

    final InputStream input = channel.getInputStream();

    final Reader reader = new InputStreamReader(input);

    final BufferedReader buffered = new BufferedReader(reader);

    while (true) {

      final String line = buffered.readLine();

      if (line == null) {
        break;
      }

      logger.info(">>> " + line);

    }

    //

    channel.disconnect();

    final int status = channel.getExitStatus();

    session.disconnect();

    logger.info("exec exit status: " + status);
View Full Code Here

  private ProcessCompletionResult runTask(String host, String command) {
    int exitCode = 0;
    ProcessCompletionResult result = null;
    // all errors will be captured in the error stream
    ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    ChannelExec channel = null;
    try {
      Session session = createSSHSession(host);
      channel = (ChannelExec) session.openChannel("exec");
      channel.setCommand(command);
      channel.setInputStream(null);
      ((ChannelExec)channel).setErrStream(errorStream);
      InputStream in=channel.getInputStream();
      //  connect actually execs the process on a remote node
      channel.connect();
      //  synchronously consume the stdout. If no stdout is available
      //  this method exits.
      consumeProcessStream(host, in, outputStream, channel);
      channel.disconnect();
      if ( channel.getExitStatus() != 0 || errorStream.size() > 0 ) {
        result =  new ProcessCompletionResult(channel.getExitStatus(), host, command, new String(errorStream.toByteArray()));
      } else {
        result =  new ProcessCompletionResult(channel.getExitStatus(), host, command);
      }
    } catch (Throwable e) {
      if ( channel == null ) {
        exitCode = -1;
      } else {
        exitCode = channel.getExitStatus();
      }
      try {
        outputStream.write(errorStream.toByteArray());
      } catch( Exception ex) {
        ex.printStackTrace();
View Full Code Here

        assertTrue(file.exists());
        assertEquals(length, file.length());
    }

    protected String readFile(String path) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        c.setCommand("scp -f " + path);
        c.connect();
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        String header = readLine(is);
        assertEquals("C0644 11 out.txt", header);
        int length = Integer.parseInt(header.substring(6, header.indexOf(' ', 6)));
        os.write(0);
        os.flush();

        byte[] buffer = new byte[length];
        length = is.read(buffer, 0, buffer.length);
        assertEquals(length, buffer.length);
        assertEquals(0, is.read());
        os.write(0);
        os.flush();

        c.disconnect();
        return new String(buffer);
    }
View Full Code Here

        c.disconnect();
        return new String(buffer);
    }

    protected String readDir(String path) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        c.setCommand("scp -r -f " + path);
        c.connect();
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        String header = readLine(is);
        assertTrue(header.startsWith("D0755 0 "));
        os.write(0);
        os.flush();
        header = readLine(is);
        assertEquals("C0644 11 out.txt", header);
        int length = Integer.parseInt(header.substring(6, header.indexOf(' ', 6)));
        os.write(0);
        os.flush();
        byte[] buffer = new byte[length];
        length = is.read(buffer, 0, buffer.length);
        assertEquals(length, buffer.length);
        assertEquals(0, is.read());
        os.write(0);
        os.flush();
        header = readLine(is);
        assertEquals("E", header);

        c.disconnect();
        return new String(buffer);
    }
View Full Code Here

TOP

Related Classes of com.jcraft.jsch.ChannelExec

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.