2016-11-16 351 views
0

我是JSch的新手,我正在使用它來連接遠程Linux機器。我正在創建一個會話並打開一個通道來在Linux機器上執行一個命令。該命令需要近半個小時才能執行並輸出。但會議在15分鐘後過期。我需要該會話處於活動狀態,直到命令完成執行。我怎樣才能讓會議持續更長時間?如何在JSch中進行會話持續時間更長?

即使使用sendKeepAliveMsg()函數也不會使會話保持活動狀態的時間不超過15分鐘。

我使用的代碼是如下

JSch jsch=new JSch(); 
java.util.Properties config = new java.util.Properties(); 
config.put("StrictHostKeyChecking", "no"); 
Session session = null; 
try 
{ 
    session = jsch.getSession(user,client_ip,22); 
    session.setPassword(secure_pwd); 
    session.setConfig(config); 
    session.connect(); 
    session.sendKeepAliveMsg(); 
    Channel channel = null; 
    channel = session.openChannel("exec"); 
    ((ChannelExec)channel).setCommand(command); 
    InputStream in = null; 
    in = channel.getInputStream(); 
    channel.connect(); 
    byte[] tmp=new byte[1024]; 
    while(true) 
    { 
     while(in.available()>0) 
     { 
      int i=in.read(tmp, 0, 1024); 
      if(i<0) 
       break; 
      System.out.print(new String(tmp, 0, i)); 
     } 
     if(channel.isClosed()) 
     { 
      System.out.println("exit-status: "+channel.getExitStatus()); 
      break; 
     } 
    } 
} 
catch(Exception e) 
{ 
    System.out.println(e.toString()); 
} 

預先感謝

+0

整個那15分鐘的命令是否連續產生輸出?或者只是間歇性的?還是隻在最後? –

+0

那15分鐘後究竟發生了什麼?你得到了什麼錯誤 –

+0

完成執行後的命令給出了結果。我需要在我的程序中解析結果。 15分鐘後程序終止,但該命令將在遠程計算機上運行 –

回答

0

您必須定期致電sendKeepAliveMsg

在開始時調用一次不起作用。

0

爲jsch空閒超時可以使用以下setServerAliveInterval

另外一些其他的連接相關的超時可被指定來調整在代碼中,例如

session.connect(5000);