Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelExec


        }
    }

    private static void sendCommand(Session session, String command)
            throws JSchException {
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(command);
        BufferedReader in;
        try {
            in = new BufferedReader(new InputStreamReader(
                    channel.getInputStream()));

            channel.connect();

            String msg = null;

            while ((msg = in.readLine()) != null) {
                System.out.println(msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        channel.disconnect();
    }
View Full Code Here


            long sessionEnd = System.currentTimeMillis();
            YSLOG.info("I00002", user, host, port, privateKey, sessionEnd - sessionStart);

            int exitStatus;
            try {
                ChannelExec channel = (ChannelExec) session.openChannel("exec");
                channel.setCommand(buildCommand(commandLineTokens, environmentVariables));
                channel.setInputStream(new ByteArrayInputStream(new byte[0]), true);
                channel.setOutputStream(output, true);
                channel.setErrStream(output, true);

                YSLOG.info("I00003", user, host, port, privateKey, commandLineTokens.get(0));
                long channelStart = System.currentTimeMillis();
                channel.connect((int) TimeUnit.SECONDS.toMillis(60));
                long channelEnd = System.currentTimeMillis();
                YSLOG.info("I00004", user, host, port, privateKey, commandLineTokens.get(0), channelEnd - channelStart);

                try {
                    while (true) {
                        if (channel.isClosed()) {
                            break;
                        }
                        Thread.sleep(100);
                    }
                    exitStatus = channel.getExitStatus();
                } finally {
                    channel.disconnect();
                }
            } finally {
                session.disconnect();
            }
            YSLOG.info("I00005", user, host, port, privateKey, commandLineTokens.get(0), exitStatus);
View Full Code Here

    }

    private SshCommandActionResult processSession(Session session, SshCommandActionDefinition definition,
        String effectiveCommand) throws Exception
    {
        ChannelExec channel = (ChannelExec) session.openChannel("exec");

        channel.setXForwarding(false);
        channel.setCommand(effectiveCommand);
        channel.setAgentForwarding(definition.isAgentForwarding());

        if (definition.getEnvironmentVariables() != null) {
            for (Map.Entry<String, String> entry : definition.getEnvironmentVariables().entrySet()) {
                channel.setEnv(entry.getKey(), entry.getValue());
            }
        }

        InputStream strOutStream = null;
        InputStream stdErrStream = null;

        try {
            strOutStream = channel.getInputStream();
            stdErrStream = channel.getErrStream();

            channel.connect();
            try {
                return processChannel(channel, definition, strOutStream, stdErrStream);
            } finally {
                channel.disconnect();
            }
        } finally {
            IOUtils.closeQuietly(strOutStream);
            IOUtils.closeQuietly(stdErrStream);
        }
View Full Code Here

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

    protected String readFile(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();
        os.write(0);
        os.flush();
        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");
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        c.setCommand("scp -r -f " + path);
        c.connect();
        os.write(0);
        os.flush();
        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);
        os.write(0);
        os.flush();

        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();
        os.write(0);
        os.flush();
        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

        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

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.