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

An example using jSch exec channel #69

Open
mhndev opened this issue Jun 24, 2020 · 1 comment
Open

An example using jSch exec channel #69

mhndev opened this issue Jun 24, 2020 · 1 comment

Comments

@mhndev
Copy link

mhndev commented Jun 24, 2020

Hi,
I need to execute a single command on a remote linux server which asks multiple questions.
Can you please give me an example ? how should I feed answers ?

@oopsaleem
Copy link

oopsaleem commented Aug 23, 2020

Hi @mhndev

        JSch jSch;
        StringBuilder remarks = new StringBuilder();
        StringBuilder error = new StringBuilder();
        StringBuilder rawData = new StringBuilder();
        try {
            Properties properties = new Properties();
            properties.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
            properties.put("StrictHostKeyChecking", "no");
            properties.put("UseDNS", "no");
            jSch = new JSch();
            Session session = jSch.getSession("user", "ip", 22);
            session.setConfig(properties);
            session.setTimeout(15 * 1_000);
            session.setPassword("password");
            session.connect();
            Channel channel = session.openChannel("shell");
            channel.connect();
            try (Expect expect = new ExpectBuilder()
                .withTimeout(10, TimeUnit.SECONDS)
                .withOutput(channel.getOutputStream())
                .withInputs(channel.getInputStream(), channel.getExtInputStream())
                .withEchoInput(rawData)
                .withEchoOutput(error)
                .withInputFilters(removeColors(), removeNonPrintable())
                .withExceptionOnFailure()
                .build()) {
                Result multiResult = expect.expect(sequence(regexp(".+"), regexp(">.|>|<.|<|#.|#|$.|$")));
                String input = multiResult.getInput();
                if (input.contains("assword:")) {
                    remarks.append("Invalid username or password.");
                } else {
                    try {
                       // execute command
                        expect.sendLine("command-making-question");
                       // expect question
                        multiResult = expect.expect(sequence(regexp(".+"), regexp("want to continue\(Yes\/No\)\?")));
                       // collect the output
                        input = multiResult.getInput(); 

                       // send answer
                        expect.sendLine("yes");
                       // expect prompt
                        multiResult = expect.expect(sequence(regexp(".+"), regexp(">.|>|<.|<|#.|#|$.|$")));
                       // collect the output
                        input = multiResult.getInput(); 
                    } catch (Exception ex) {
                        error.append(String.format("Error with [%s]: %s. %s", command, ex.getMessage(), ex.getCause() == null ? "" : "Cause: " + ex.getCause()));
                    }
                }
            } finally {
                channel.disconnect();
                session.disconnect();
            }
            if (rawData.length() <= 0 )
                  return new CommandWrapper(remarks.toString(), error.toString());
            else 
                  return new CommandWrapper(rawData.toString());
        } catch (Exception e) {
            return new CommandWrapper(remarks.toString(), String.format("Error: %s %s %s",
                e.getMessage(),
                e.getCause() == null ? "" : "Cause: " + e.getCause(),
                error.toString()));
        }

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