Question

I am having some issues using JSCH and sending commands to shell.

I have a console GUI Window setup and system.out has been redirected to the TextArea and this works fine, however i am unable to input any commands

Here is the connect code for the session

    this.channel=session.openChannel("shell");

    PipedInputStream pip = new PipedInputStream(40);
    this.channel.setInputStream(pip);

    PipedOutputStream pop = new PipedOutputStream(pip);
    PrintStream print = new PrintStream(pop);

    this.channel.setOutputStream(System.out);

    print.println("ls"); 

    this.channel.connect(3*1000);

This works fine, and runs the ls command and displays the output, however if i now want to run more commands these dont work.

i have a TextBox setup and a "Send" button to send these commands to the server coded below.

    String send = jServerInput.getText();

    try {
        PipedInputStream pip = new PipedInputStream(40);
        //this.channel.setInputStream(pip);

        PipedOutputStream pop = new PipedOutputStream(pip);
        PrintStream print = new PrintStream(pop);
        //this.channel.setOutputStream(System.out);
        //System.out.println(send);
        print.println(send);
    } catch (IOException iOException) {
    }

However hitting the "Send" Button does nothing. I am clearly missing something simple

Was it helpful?

Solution

I found that i needed to declare PrintStream as a Private so

 private PrintStream print;

Then after i had created the initial PrintStream as

 print = new PrintStream(pop); 

I was able to access it in other parts of the programme rather than create new ones, so all i needed in my send command in the end was

 String send = jServerInput.getText();
 print.println(send);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top