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

Obtaining the device list

The following code retrieves the list of network interfaces that can be opened with Pcap.live(Interface, LiveOptions()).

PreviousLoggingNextOpening an adapter and capturing the packets

Last updated 3 years ago

Was this helpful?

Note: There may be network devices that cannot be opened by the process calling Service.interfaces(), because, for example, that process does not have sufficient privileges to open them for capturing; if so, those devices will not appear on the list.

Docs, .

public static void main(String[] _args) throws ErrorException {
  var service = Service.Creator.create("PcapService");
  Iterator<Interface> devices = service.interfaces().iterator();
  while (devices.hasNext()) {
    Interface device = devices.next();
    System.out.println("Name             : " + device.name());
    System.out.println("Description      : " + device.description());
    System.out.println("Flags            : " + device.flags());
    if (device.addresses() != null) {
      System.out.println("Addresses        : ");
      Iterator<Address> addresses = device.addresses().iterator();
      while (addresses.hasNext()) {
        Address address = addresses.next();
        System.out.println("\tAddress      : " + address.address());
        System.out.println("\tNetmask      : " + address.netmask());
        System.out.println("\tBroadcast    : " + address.broadcast());
        System.out.println("\tDestination  : " + address.destination());
      }
    }
    System.out.println();
  }
}
fun main() {
  val service = Service.Creator.create("PcapService")
  val devices: Iterator<Interface> = service.interfaces().iterator()
  while (devices.hasNext()) {
    val device = devices.next()
    println("Name             : " + device.name())
    println("Description      : " + device.description())
    println("Flags            : " + device.flags())
    if (device.addresses() != null) {
      println("Addresses        : ")
      val addresses: Iterator<Address> = device.addresses().iterator()
      while (addresses.hasNext()) {
        val address: Address = addresses.next()
        System.out.println("\tAddress      : " + address.address())
        System.out.println("\tNetmask      : " + address.netmask())
        System.out.println("\tBroadcast    : " + address.broadcast())
        System.out.println("\tDestination  : " + address.destination())
      }
    }
    println()
  }
}
def main(args: Array[String]): Unit = {
  val service = Service.Creator.create("PcapService")
  val devices = service.interfaces.iterator
  while ( {
    devices.hasNext
  }) {
    val device = devices.next
    System.out.println("Name             : " + device.name)
    System.out.println("Description      : " + device.description)
    System.out.println("Flags            : " + device.flags)
    if (device.addresses != null) {
      System.out.println("Addresses        : ")
      val addresses = device.addresses.iterator
      while ( {
        addresses.hasNext
      }) {
        val address = addresses.next
        System.out.println("\tAddress      : " + address.address)
        System.out.println("\tNetmask      : " + address.netmask)
        System.out.println("\tBroadcast    : " + address.broadcast)
        System.out.println("\tDestination  : " + address.destination)
      }
    }
    System.out.println()
  }
}
(defn print_addresses [addresses]
  (println "Addresses        :")
  (while (.hasNext addresses)
    (def address (.next addresses))
    (println "\tAddreess      :" (.address address))
    (println "\tNetmask       :" (.netmask address))
    (println "\tBroadcast     :" (.broadcast address))
    (println "\tDestination   :" (.destination address))))

(defn -main
  [& args]
  (def service (. Service$Creator (create "PcapService")))
  (def devices (.iterator (.interfaces service)))
  (while (.hasNext devices)
    (def device (.next devices))
    (println "Name             :" (.name device))
    (println "Description      :" (.description device))
    (println "Flags            :" (.flags device))
    (if (some? (.addresses device))
      (print_addresses (.iterator (.addresses device))))))
: libpcap
javadoc