1

GUI アプリケーションを開く ssh コマンドを実行する必要があります。Jsch にコマンドを実行させることができ、クライアント マシンに GUI が表示されました。私の問題は、20 の Jsch チャネルを超えないように見えることです。サーバーには、ユーザーが作成できるssh接続の数を制御する設定があり、ここでは20のようです。理解できないのは、既存の接続を再利用して別のコマンドを実行する方法です....

コマンドを2つの異なる方法で実行しようとしました:

EXAMPLE Command:  

String command = "cd /home/test;xterm ";
String command = "cd /home/test;nedit myfile.txt ";

「一方向」) 各実行コマンドは、新しい Jsch チャネルを作成します。

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 接続が作成されているようです。そのため、まだ 20 の ssh 接続制限があります。]

そのため、サーバーは最大 20 の ssh 接続しか許可していないようです。しかし、なぜ「他の方法」ではうまくいかないのでしょうか?

そのため、GUI アプリケーションを閉じると、ssh 接続が解放されないように見えます。

私の問題は、すべてのコマンドが GUI アプリケーションを開くため、そのアプリケーションがいつ閉じられてチャネル接続が閉じられるかがわからないことです。

新しい ssh 接続を作成するのではなく、既存の接続を使用して新しいコマンドを送信できるようにする必要があると考えて、「別の方法」の方法を作成しました。明らかに、そのようには機能しません。

connect(command) が呼び出されたときに、1 つの ssh 接続を使用して別のコマンドを実行するにはどうすればよいですか? それはJschで可能ですか?

4

1 に答える 1

0

解決しない! 以下はある程度機能します。Jschチャネル接続が閉じられると、「xterm」に表示情報がなくなるという事実が隠されます。

=====================================

チャネルが切断されたときに GUI が消えないようにするには、コマンドの前に「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();
}
于 2012-07-18T17:21:10.963 に答える