2014-10-30 187 views
0

我目前有一個問題,實現jsch庫來從ZyXel開關中獲取信息。程序本身會抓取一些信息來確認交換機的類型,然後上傳正確的固件和配置。使用jsch時緩衝區問題

我的問題,對我來說,似乎是一個緩衝區問題。我沒有問題發送命令,但當我發送它時,根據我運行它的時間或頻率,我要麼獲得一半的信息,我應該得到或全部。我認爲這是因爲有時緩衝區並不會全部進入ByteArrayInputStream中,而是在這個時候我處於迷路狀態。我想知道是否有人可以指出我正確的方向對我的錯誤。我認爲這是一個基本的InputStream或JSF文檔問題誤解

謝謝!..我的代碼如下。

package ssh; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import com.jcraft.jsch.*; 

public class ssh { 
    private static String user = "admin"; 
    private static String host = "192.168.1.1"; 
    private static String password = "1234"; 
    private static String command = ""; 
    public static void startAutomation() { 
     JSch jsch = new JSch(); 
     Session session = null; 
     OutputStream output = null; 
     Channel channel = null; 
     try { 

      session = jsch.getSession(user,host,22); 
      session.setPassword(password); 
      session.setConfig("StrictHostKeyChecking","no"); 

      session.connect(); 
      channel = session.openChannel("shell"); 

      command = "show system-information\n"; 

      output = runCommand(command, session, channel); 
      String test = "NOTHING"; 
      if (output.toString().contains("ES-2024A")) { 
       test = "true"; 
       command = "show run\n"; 
       output = runCommand(command,session,channel); 
      } else { 
       test = "false"; 
      } 
      System.out.println(test + " This is a 2024A"); 

     } catch (JSchException | InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      channel.disconnect(); 
      session.disconnect(); 
     } 
    } 

public static OutputStream runCommand(String c,Session session,Channel channel) throws InterruptedException, JSchException{ 
    InputStream is = new ByteArrayInputStream(c.getBytes()); 
    channel.setInputStream(is); 

    OutputStream outputInfo = new ByteArrayOutputStream(); 
    channel.setOutputStream(outputInfo); 

    channel.connect(15*1000); 

    try { 
     is.close(); 
     outputInfo.flush(); 
     outputInfo.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return outputInfo; 
} 

} 

回答

0

我在類似項目上工作,現在我創建了全局輸出流,不需要等待任何東西。 我重新寫的OutputStream使用的JTextArea作爲輸出

private java.util.List<Character> buffer = new ArrayList<Character>(); 
public class AreaOutputStream extends OutputStream { 
private JTextArea textArea; 

public AreaOutputStream(JTextArea textArea) { 
    this.textArea = textArea; 
} 

@Override 
public synchronized void write(int b) throws IOException { 
    // collect all character into the buffer 
    buffer.add((char) 
    // on '\n' character append to output window 
    if (String.valueOf((char) b).equals("\n")) { 
      StringBuilder sb = new StringBuilder(buffer.size()); 
      for (Character c : buffer) sb.append(c); 
      String line = sb.toString(); 
      // filter output from special characters 
      //and replace invitation '[[email protected]~]$ ' with current time 
      line = line.replaceAll(" \r", ""); 
      line = line.replaceAll("\r", ""); 
      line = line.replaceAll(String.valueOf(Pattern.compile("\\[[0-9][^m ]*m")), ""); 
      line = line.replaceAll(String.valueOf(Pattern.compile("\\[.*@[^$]*\\$ ")), DateFormat.getTimeInstance().format(java.lang.System.currentTimeMillis()) + " "); 
      line = line.replaceAll(String.valueOf(Pattern.compile("\u001B")), ""); 
      if (!line.matches("(.*)Last login(.*)from(.*)\n")) { 
       textArea.append(line); 
       textArea.setCaretPosition(textArea.getDocument().getLength()); 
      } 
     buffer.clear(); 
    } 

} 
} 

因此,在部分使用此輸出流,你建立自己的頻道足夠集以下:

channel.setOutputStream(new AreaOutputStream(TEXT_AREA_NAME)) 

所以,你可以創建另一個渠道執行命令並將其設置爲此類的輸出 - 所有這些都將顯示在JTextArea中。

BTW現在,我嘗試創建的InputStream在一個通道發送的命令(聯繫人請「mail.ru」替換「domain.name」)

希望這有助於和對不起我的英語。