2015-11-02 135 views
0

我有這樣的:JSch多個命令問題?

ChannelExec channel1 = (ChannelExec) session.openChannel("exec"); 
String command1 = "/opt/deluge/latest/bin/deluge-console -c ./.config/deluge"; 
String commandCon = "connect 127.0.0.1:2035"; 
String location = channelSftp.getHome(); 
String command2 = "add " + location + "/torrent1.torrent"; 
channel1.setCommand(command1 + "\n" + commandCon + "\n" + command2); 
channel1.connect(); 

但不加入我試圖添加的文件。

完整的命令我需要的是

/opt/deluge/latest/bin/deluge-console -c ./.config/deluge 

然後,一旦我們開始deluge-console,它

connect 127.0.0.1:2035 

連接到後臺程序,然後

add /home/hd1/testuser/torrent1.torrent 

我知道我的命令是正確的,因爲我可以測試它們沒有使用Java,但由於某種原因,我沒有得到我在上面的代碼中所期望的結果。

回答

0

指定使用.setCommand的命令是由shell執行的命令。

這些都是不等同於「行」,你SSH終端上輸入。

因此connect命令將在deluge-console退出後執行,但從未發生過,因爲deluge-console等待其標準輸入上的命令。


解是:

  • 管命令到deluge-console等:

    (echo connect ... ; echo add ...) | deluge-console ... 
    
  • 使用標準輸入訂閱命令到開始deluge-consolechannel1.getOutputStream

    Sudo.java JSc h例子。