2014-10-30 181 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”)

希望这有助于和对不起我的英语。