我需要运行ssh命令来打开GUI应用程序。我能够让Jsch运行这些命令,并且GUI显示在客户机上。我的问题是我似乎不能超过20个Jsch频道。我意识到服务器有一个设置来控制用户可以建立的ssh连接的数量,这里似乎是20。我不能理解的是,如何重用现有的连接,但运行不同的命令……
我尝试以两种不同的方式运行命令:
EXAMPLE Command:
String command = "cd /home/test;xterm ";
String command = "cd /home/test;nedit myfile.txt ";private void connect (String command) {
Channel channel = session.getChannel("shell");
channel.setXForwarding(true);
StringBufferInputStream reader = new StringBufferInputStream(command + " \n");
channel.setInputStream(reader);
channel.connect();
}有效,但达到20个ssh连接限制。
或
“另一种方式”)尝试重用通道来运行一个新命令,其中通道是一个全局变量:
int numruns =0;
private void connect (String command, int channelId) {
String cmd = command + " \n";
if (channel == null) {
numruns = 0;
channel = session.openChannel("shell");
channel.setXForwarding(true);
channel.connect();
stdIn = channel.getOutputStream();
stdOut = channel.getInputStream();
} else {
channel.connect(channelId);
}
((OutputStream)stdIn).write(cmd.getBytes());
stdIn.flush();
numruns++;
}所以当我关闭我的图形用户界面应用程序时,它似乎没有释放ssh连接,因为它仍然认为我已经耗尽了,所以我在channel.connect()上获得了JschException;
我的问题是,所有的命令都会打开GUI应用程序,所以我无法判断该应用程序何时关闭以关闭通道连接。
我编写了"other way“方法,认为它不会创建新的ssh连接,但应该允许我使用现有的连接,但发送一个新命令。显然,它不是这样工作的。
如何在调用connect( command )时使用一个ssh连接来运行不同的命令?对于Jsch,这是可能的吗?
发布于 2012-07-19 01:21:11
没有解决!下面的方法在一定程度上是可行的。它隐藏了这样一个事实:一旦Jsch通道连接关闭,任何"xterm“都不再具有显示信息。
=====================================
为了防止在断开通道时图形用户界面消失,需要在命令前加上"nohup“或"xterm -hold -e”。
所以..。
命令示例:
String command = "cd /home/test;xterm";
String command = "cd /home/test;nohup nedit myfile.txt"; // this will only keep the GUI opened
String command = "cd /home/test;xterm -hold -e gedit myfile.txt"; // this one keeps the xterm window that kick off the program opened因此,在对命令进行更改后,添加了Thread.sleep(1000),以便在断开通道之前给应用程序时间。
这似乎起作用了!
private void connect (String command) {
Channel channel = session.getChannel("shell");
channel.setXForwarding(true);
StringBufferInputStream reader = new StringBufferInputStream(command + " \n");
// or use
// ByteArrayInputStream reader = new ByteArrayInputStream((command + " \n").getBytes());
channel.setInputStream(reader);
channel.setOuputStream(System.out);
channel.connect();
try {
Thread.sleep(1000); // give GUI time to come up
} catch (InterruptedException ex) {
// print message
}
channel.disconnect();
}https://stackoverflow.com/questions/11526478
复制相似问题