Search code examples
javasshopensshmatcher

I have a problem with the java SSHClient class, specifically the Expect method does not return data as espected, what could be happening?


I have used the SSHClient class of java to connect to a Juniper router, when entering the console in the putty console it returns the result without problem, but when doing it from the Java code it does not save what is returned by the command, or does not print it, Could you tell me what to do?

weird inconvenience

I share the ssh class that I am using to guide you in case you encounter any problems:

package com.mycompany;

import static net.sf.expectit.matcher.Matchers.contains;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Shell;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.sf.expectit.Expect;
import net.sf.expectit.ExpectBuilder;

public class sshj {

    /**
     * Launch Commands to Provisioning
     * 
     * @param host
     * @param port
     * @param user
     * @param password
     * @param commands
     * @param splitBy
     * @return result
     */
    public static String launchCommands(String host, Integer port, String user, String password, String commands,
            String commandsSplitBy, String expectSplitBy, Integer timeoutInSeconds) {
        String result = "";
        StringBuilder wholeBuffer = new StringBuilder();
        SSHClient mSSSHClient = new SSHClient();
        mSSSHClient.addHostKeyVerifier(new PromiscuousVerifier());

        Session mSession = null;
        Shell mShell = null;
        Expect mExpect = null;

        String[] splitCommands = commands.split(commandsSplitBy);

        try {
            mSSSHClient.connect(host, port);
            mSSSHClient.authPassword(user, password);

            mSession = mSSSHClient.startSession();
            mShell = mSession.startShell();

            mExpect = new ExpectBuilder()
                    .withOutput(mShell.getOutputStream())
                    .withInputs(mShell.getInputStream())
                    .withEchoInput(wholeBuffer)
                    .withEchoOutput(wholeBuffer)
                    // .withEchoInput(System.out)
                    // .withEchoOutput(System.err)
                    .withExceptionOnFailure()
                    .withTimeout(timeoutInSeconds, TimeUnit.SECONDS).build();

            // When expectSplitBy is equals to ""
            if ("".equalsIgnoreCase(expectSplitBy)) {
                for (String commandExpect : splitCommands) {

                    mExpect.sendLine(commandExpect);

                }

            } else { // When expectSplitBy is not equals to ""

                for (String commandExpect : splitCommands) {
                    String[] commandExpectSplit = commandExpect.split(expectSplitBy);
                    mExpect.sendLine(commandExpectSplit[0]);
                    mExpect.expect(contains(commandExpectSplit[1]));
                }

            }

            mShell.close();
            mSession.close();
            mSSSHClient.disconnect();

            result = wholeBuffer.toString();
        } catch (IOException e) {
            result = wholeBuffer.toString().concat(" The Exception is> ").concat(e.toString());
            e.printStackTrace();

            try {
                mExpect.sendLine("exit");
                mShell.close();
                mSession.close();
                mSSSHClient.disconnect();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        return result;
    }

}

Solution

  • I found the problem, is that the command sent contains some strange characters at the beginning that look like a blank space, and I could eliminate them and the functionality of the class is executed without problem.

    I tagged mule because it's where I'm working with this class, which I know is from a third party, that's why I shared all the code so you could do tests. I am very sorry if you do not find my question clear.