Search code examples
javatcpdump

Monitor tcpflow output with Java?


How can I monitor tcpflow output with Java? I've tried the following but it doesn't work with the tcp flow -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();
}

How come the output of tcpflow isnt read? However if I exec the tcpflow -h the output is read.


Solution

  • Found a work around by tailing a log file from 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();
    }
    

    And the class that trails it.

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