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

implement interactive shell (with tested solution) #87

Open
terefang opened this issue Apr 12, 2019 · 2 comments
Open

implement interactive shell (with tested solution) #87

terefang opened this issue Apr 12, 2019 · 2 comments

Comments

@terefang
Copy link

use case: chat on interactive shell (session preserving)

solution: add the following code to SessionDelegate

Channel shellChannel;
Expect4j expect;

    String shell(String command, String terminator)
    {
        expect.send(command);
        return returnShellOutput(terminator);
    }

    String returnShellOutput(String terminator)
    {
        final StringBuffer sb = new StringBuffer();
        BufferChangeLogger bcl = new BufferChangeLogger() {
            @Override
            void bufferChanged(char[] newData, int numChars) {
                sb.append(newData,0,numChars);
            }
        }
        expect.registerBufferChangeLogger(bcl);
        expect.expect(terminator);
        expect.unregisterBufferChangeLogger(bcl);
        int off = sb.toString().lastIndexOf(terminator);
        if(off>0)
        {
            return sb.toString().substring(0, off);
        }
        else
        {
            return sb.toString();
        }
    }

    def newShell(int timeout) {
        if(shellChannel==null)
        {
            connect();
            try
            {
                shellChannel=getSession().openChannel("shell");
                shellChannel.setPty(false);
                expect = new Expect4j(shellChannel.getInputStream(), shellChannel.getOutputStream());
                shellChannel.connect(timeout);
            }
            catch(Exception xe)
            {
                println(xe);
            }
        }
    }

    def exitShell() {
        disconnectShell();
    }

    def disconnectShell() {
        if(shellChannel!=null)
        {
            expect.close();
            shellChannel.close();
        }
        shellChannel=null;
    }

usage:

remoteSession {
   newShell(10000);
   returnShellOutput('prompt#');
   def chatOutput;
   chatOutput = shell('ls -la', 'prompt#'));
   chatOutput = shell('cd /tmp', 'prompt#'));
   chatOutput = shell('ls -la', 'prompt#'));
   exitShell();
}
@terefang terefang changed the title implement interactive shell implement interactive shell (with tested solution) Apr 12, 2019
@northmirko
Copy link

Hi @terefang , great idea.
I need this feature, but didn't managed to handle dependencies required to produce jar archive.
Can you show us how to put dependencies in gradle.build or else where ?

@terefang
Copy link
Author

AFAIK the only required dependency should be "com.github.cverges:expect4j:1.9"

sorry i am a maven guy

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