Pcap
  • Introduction
  • Getting Started
    • Dependency management
    • Installing Libpcap or Npcap
    • Run as non root user
    • Logging
    • Obtaining the device list
    • Opening an adapter and capturing the packets
    • Capturing the packets without the callback
    • Capture statistics
    • Filtering the traffic
    • Write the packets to a capture file
    • Read packets from a capture file.
    • Sending packets
    • I/O Multiplexing
    • Restricted Method
  • Developer Guide
    • Branches to look
    • Build from Source
    • Notes
  • Packet Structure
    • Packet Header
    • Packet Buffer
  • Packet Codec
    • Using packet codec
    • Adding protocol support
  • Others
  • Thanks to
  • Fork me on Github
Powered by GitBook
On this page

Was this helpful?

  1. Getting Started

Write the packets to a capture file

Capture the packets and write them to a capture file.

PreviousFiltering the trafficNextRead packets from a capture file.

Last updated 4 years ago

Was this helpful?

Note: Pcap.dumpOpen(String) will override if the "savefile" already exists; If you need to add a captured packet to the existing "savefile" you can use Pcap.dumpOpenAppend(String) instead.

Docs, .

public static void main(String[] _args)
    throws ErrorException, PermissionDeniedException, PromiscuousModePermissionDeniedException,
        TimestampPrecisionNotSupportedException, RadioFrequencyModeNotSupportedException,
        NoSuchDeviceException, ActivatedException, InterfaceNotUpException,
        InterfaceNotSupportTimestampTypeException {
  var service = Service.Creator.create("PcapService");
  try (var pcap = service.live(service.interfaces(), new DefaultLiveOptions())) {
    try (var dumper = pcap.dumpOpen("savefile.pcap")) {
      try {
        pcap.loop(
            10,
            (args, header, buffer) -> {
              System.out.println("Args     : " + args);
              System.out.println("Header   : " + header);
              System.out.println("Packet   : " + buffer);
              dumper.dump(header, buffer);
            },
            "Hello pcap!");
      } catch (BreakException e) {
        System.err.println(e.getMessage());
      } catch (ErrorException e) {
        System.err.println(e.getMessage());
      }
    }
  }
}
fun main() {
  val service = Service.Creator.create("PcapService")
  val pcap = service.live(service.interfaces(), DefaultLiveOptions())
  val dumper = pcap.dumpOpen("savefile.pcap")
  try {
    pcap.loop(
      10,
      { args: String, header: PacketHeader, buffer: PacketBuffer ->
        println("Args     : $args")
        println("Header   : $header")
        println("Packet   : $buffer")
        dumper.dump(header, buffer)
      },
      "Hello pcap!"
    )
  } catch (e: BreakException) {
    System.err.println(e.message)
  } catch (e: ErrorException) {
    System.err.println(e.message)
  }
  dumper.close()
  pcap.close()
}
def main(_args: Array[String]): Unit = {
  val service = Service.Creator.create("PcapService")
  val pcap = service.live(service.interfaces(), new DefaultLiveOptions())
  val dumper = pcap.dumpOpen("savefile.pcap");
  try pcap.loop(10, (args: String, header: PacketHeader, buffer: PacketBuffer) => {
    println("Args     : ", args)
    println("Header   : ", header)
    println("Packet   : ", buffer)
    dumper.dump(header, buffer)
  }, "Hello pcap!")
  catch {
    case e: BreakException =>
      System.err.println(e.getMessage)
    case e: ErrorException =>
      System.err.println(e.getMessage)
  }
  dumper.close()
  pcap.close()
}

: libpcap
javadoc