解析 JSCH 对列表的响应

问题描述 投票:0回答:1

我正在尝试解析来自 JSCH 的响应(这是一个包含多行响应的 grep cmd),并且我正在尝试将每一行添加到列表中(以供以后处理)。如果这意味着什么,我正在使用 shell 通道。这是功能:

static public List<String> waitForPrompt(ByteArrayOutputStream outputStream, String prompt) throws Exception {
    int retries = 700
    List<String> lines = new ArrayList<>()
    for (int x = 1; x < retries; x++) {
        TimeUnit.SECONDS.sleep(1)
        String responseString = outputStream.toString()
        responseString.eachLine { line ->
            lines.add(line.replaceAll("(\\x9B|\\x1B\\[)[0-?]*[ -\\/]*[@-~]", ""))
        }
        if (lines.find { it.contains(prompt) }) {
            return lines
        }
        outputStream.reset()
    }
    throw new Exception("Prompt failed to show after specified timeout")
}

我确实看到了两个条目,但是创建的列表只有一个项目(整个响应;()。为什么?

groovy soapui jsch
1个回答
0
投票

长话短说,不要使用 shell,使用 exec 通道。我停止使用提示,而是这样做:

private String getChannelOutput(Channel channel, InputStream in) throws IOException{
    byte[] buffer = new byte[1024]
    StringBuilder strBuilder = new StringBuilder();
    String line = ""
    while(!channel.isClosed()){
        while (in.available() > 0) {
            int i = in.read(buffer, 0, 1024)
            if (i < 0) {
                log.info('found no response')
                break;
            }
            strBuilder.append(new String(buffer, 0, i))
            Thread.sleep(100)
        }
    }
    return strBuilder.toString()        
}
© www.soinside.com 2019 - 2024. All rights reserved.