Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when long long timeout and no expect msg and shell execed failure, how to throw exec exception quickly? #55

Open
juaby opened this issue Jul 25, 2018 · 2 comments

Comments

@juaby
Copy link

juaby commented Jul 25, 2018

public static void main(String[] args) throws JSchException, IOException {
        final InputAppendable inputBuffer = new InputAppendable();
        final OutputAppendable outputBuffer = new OutputAppendable();
        JSch jSch = new JSch();
        String prvKeyPath = "~/.ssh/id_rsa";
        jSch.addIdentity(prvKeyPath);
        Session session = jSch.getSession("who", "localhost");
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        Channel channel = session.openChannel("shell");
        channel.connect();

        //inputBuffer.setLength(0);
        //outputBuffer.setLength(0);

        Expect expect = new ExpectBuilder()
                .withOutput(channel.getOutputStream())
                .withInputs(channel.getInputStream(), channel.getExtInputStream())
                .withEchoInput(inputBuffer)
                .withEchoOutput(outputBuffer)
                .withInputFilters(removeColors(), removeNonPrintable())
                .withExceptionOnFailure()
                .withAutoFlushEcho(true)
                .build();
        try {
            expect.expect(contains("$"));
            expect.sendLine("ping1 -c 50 jd.com");
            long timeout = 99999999999L;
            boolean result = expect.withTimeout(timeout, TimeUnit.SECONDS).expect(contains("ping statistics")).isSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            expect.close();
            channel.disconnect();
            session.disconnect();
        }
    }
@agavrilov76
Copy link
Owner

Not sure what do you mean by "shell execed failure"? What are you trying to achieve?

@juaby
Copy link
Author

juaby commented Jul 26, 2018

It takes a long time for shell to perform a task, set a long timeout time, and don't wait for the final timeout when the shell execution fails, how to immediately throw the exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants