Obtaining the device list

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

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: libpcap, javadoc.

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();
  }
}

Last updated