问题
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