本文整理汇总了Java中org.pcap4j.core.Pcaps.findAllDevs方法的典型用法代码示例。如果您正苦于以下问题:Java Pcaps.findAllDevs方法的具体用法?Java Pcaps.findAllDevs怎么用?Java Pcaps.findAllDevs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.pcap4j.core.Pcaps
的用法示例。
在下文中一共展示了Pcaps.findAllDevs方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: show
import org.pcap4j.core.Pcaps; //导入方法依赖的package包/类
void show(Config config) {
try {
List<PcapNetworkInterface> devices = Pcaps.findAllDevs();
List<String> names = devices.stream().map(PcapNetworkInterface::getName).collect(Collectors.toList());
ChoiceDialog<String> dialog = new ChoiceDialog<>(names.get(0), names);
dialog.initModality(Modality.WINDOW_MODAL);
dialog.setResizable(false);
dialog.setHeaderText("Choose a network interface:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
config.setDevice(dialog.getSelectedItem());
MainWindow app = new MainWindow("Knitcap");
app.start(config);
}
} catch (PcapNativeException e) {
e.printStackTrace();
}
}
示例2: MacAddressHelper
import org.pcap4j.core.Pcaps; //导入方法依赖的package包/类
private MacAddressHelper() {
try {
_localPcapNetworkInterfaces = Pcaps.findAllDevs();
_executor = Executors.newScheduledThreadPool(_threadCount);
_IPv6_BROADCAST_IPADDRESS_PREFIX = Inet6Address.getByName("FF02::1:FF00:0000").getAddress();
_IPv6_BROADCAST_MACADDRESS_PREFIX = MacAddress.getByName("33:33:ff:00:00:00").getAddress();
_localAddresse2MacAddress = new HashMap<>();
Enumeration<NetworkInterface> localNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (localNetworkInterfaces.hasMoreElements()) {
NetworkInterface nwInterface = localNetworkInterfaces.nextElement();
byte[] mac = nwInterface.getHardwareAddress();
Enumeration<InetAddress> addresses = nwInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress currentIp = addresses.nextElement();
if (mac == null) {
System.out.println("Can't find mac address for local ip=" + currentIp);
_localAddresse2MacAddress.put(currentIp, null);
}
else {
// although jdk said, the returned value is a mac address, but actually this may fail on some local mac address
// the length is NOT 6 bytes
if (mac.length != MacAddress.SIZE_IN_BYTES) {
_localAddresse2MacAddress.put(currentIp, null);
System.out.println(String.format("Found invalid mac address ip=%s,mac=%s", currentIp, ByteArrays.toHexString(mac,
":")));
}
else {
_localAddresse2MacAddress.put(currentIp, MacAddress.getByAddress(mac));
}
}
}
}
System.out.println(String.format("Mac Address helper init done localips=%d, threadPool=%d, readTimeout(ms)=%d, waitResponse(ms)" +
"=%d, waitReceiveTaskStart(ms)=%d",
_localAddresse2MacAddress.size(), _threadCount,
_readTimeoutInMillSeconds, _waitResponseTimeoutInMillSeconds, _waitReceiveTaskStartRunningInSeconds));
_initted = true;
}
catch (Throwable e) {
_initError = e;
}
}
示例3: getLocalAddr
import org.pcap4j.core.Pcaps; //导入方法依赖的package包/类
public static void getLocalAddr() throws InterruptedException, PcapNativeException, UnknownHostException, SocketException,
ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
if (Settings.getDouble("autoload", 0) == 1) {
addr = InetAddress.getByName(Settings.get("addr", ""));
return;
}
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final JFrame frame = new JFrame("Network Device");
frame.setFocusableWindowState(true);
final JLabel ipLab = new JLabel("Select LAN IP obtained from Network Settings:", JLabel.LEFT);
final JComboBox<String> lanIP = new JComboBox<String>();
final JLabel lanLabel = new JLabel("If your device IP isn't in the dropdown, provide it below.");
final JTextField lanText = new JTextField(Settings.get("addr", ""));
ArrayList<InetAddress> inets = new ArrayList<InetAddress>();
for (PcapNetworkInterface i : Pcaps.findAllDevs()) {
for (PcapAddress x : i.getAddresses()) {
InetAddress xAddr = x.getAddress();
if (xAddr != null && x.getNetmask() != null && !xAddr.toString().equals("/0.0.0.0")) {
NetworkInterface inf = NetworkInterface.getByInetAddress(x.getAddress());
if (inf != null && inf.isUp() && !inf.isVirtual()) {
inets.add(xAddr);
lanIP.addItem((lanIP.getItemCount() + 1) + " - " + inf.getDisplayName() + " ::: " + xAddr.getHostAddress());
System.out.println("Found: " + lanIP.getItemCount() + " - " + inf.getDisplayName() + " ::: " + xAddr.getHostAddress());
}
}
}
}
if (lanIP.getItemCount() == 0) {
JOptionPane.showMessageDialog(null, "Unable to locate devices.\nPlease try running the program in Admin Mode.\nIf this does not work, you may need to reboot your computer.",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
lanIP.setFocusable(false);
final JButton start = new JButton("Start");
start.addActionListener(e -> {
try {
if (lanText.getText().length() >= 7 && !lanText.getText().equals("0.0.0.0")) { // 7 is because the minimum field is 0.0.0.0
addr = InetAddress.getByName(lanText.getText());
System.out.println("Using IP from textfield: " + lanText.getText());
} else {
addr = inets.get(lanIP.getSelectedIndex());
System.out.println("Using device from dropdown: " + lanIP.getSelectedItem());
}
Settings.set("addr", addr.getHostAddress().replaceAll("/", ""));
frame.setVisible(false);
frame.dispose();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
});
frame.setLayout(new GridLayout(5, 1));
frame.add(ipLab);
frame.add(lanIP);
frame.add(lanLabel);
frame.add(lanText);
frame.add(start);
frame.setAlwaysOnTop(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (frame.isVisible())
Thread.sleep(10);
}