JSCH Channel Shell, Input / Output

帅比萌擦擦* 提交于 2019-12-12 00:35:50

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/19490030/jsch-channel-shell-input-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!