P
P
Pcap
Search…
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.
Java
Kotlin
Scala
Clojure
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))))))
Copy link