java - 使用 Java 监视 tcpflow 输出?

标签 java tcpdump

如何使用 Java 监控 tcpflow 输出? 我尝试了以下方法,但它不适用于 tcp 流 -c arg。

Process process;

try {
    process = Runtime.getRuntime().exec("tcpflow -c");
    InputStreamReader ir = new 
    InputStreamReader(process.getInputStream());
    LineNumberReader input = new LineNumberReader(ir);
    String line;
    while ((line = input.readLine()) != null) {
        System.out.println("Output: " + line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

为什么 tcpflow 的输出没有被读取?但是,如果我执行 tcpflow -h,则会读取输出。

最佳答案

通过跟踪来自 tcpflow 的日志文件找到了解决方法。

public static void main(String[] args) {
    File log = new File("packets.log");
    PacketListener listener = new PacketListener();
    Tailer tailer = Tailer.create(log, listener, 2500);
    tailer.run();
}

以及跟踪它的类。

public class PacketListener extends TailerListenerAdapter {

    @Override
    public void handle(String line) {
        System.out.println("Inbound: " + line);
    }

}

关于java - 使用 Java 监视 tcpflow 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358541/

相关文章:

java - 为什么我无法通过 App Engine 将大于 100KByte 的文件上传到 Google Cloud Storage?

java - 为什么不同组织的javax.annotation不同

javax.命名.NamingException : Cannot create resource instance

network-programming - 在嗅探时查找数据包的方向

android - 交叉编译 - 用于 x86 的 tcpdump

java - Maven 创建类文件 50.0,应该是 49.0

java - 计算不相交的有向字母

mysql - 使用 tcpdump 记录繁忙的网络流量

c - 在不断增长的日志文件上使用 tcpdump

linux - tcpdump 可以跳过 header 吗