Java accurate get’ MAC/IP4/IP6/subnet mask/gateway/adapter/computer information plug-in

Table of Contents

Java native API:

C++ native way to start getting

ip_if_filter: quoted from ipifcons.h

family: quoted from ws2def.h

Download

colorful1.2 framework

Tutorial

A simple try


Java native API:

You can use: NetworkInterface to obtain computing network interface information (the following figure traverses all network interfaces)

 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                   // if (!address.isLoopbackAddress() & amp; & amp; address.isSiteLocalAddress()) {
                    console.info(networkInterface.getInterfaceAddresses());
                        console.Info(networkInterface.getInterfaceAddresses());
                  // }
                }
            }

Comparison

This method of acquisition is not necessarily accurate. There are several influencing factors:

  1. Permissions: In some operating systems, obtaining information about a network interface may require special permissions. Without these permissions, it may not be possible to obtain all network interface information.

  2. Virtual and hidden network interfaces: Some systems may have virtual network interfaces or hidden network interfaces, which may not be returned by NetworkInterface.getNetworkInterfaces().

  3. Java security policy: If your Java program runs in an environment with a security manager, you may need NetPermission("getNetworkInformation") permission to obtain the network Interface information. Otherwise, NetworkInterface.getNetworkInterfaces() may simply return an empty Enumeration.

  4. Network interface status: If network interfaces are in an inactive state, such as not connected to the network, they may not be returned by NetworkInterface.getNetworkInterfaces().

Start acquisition in C++ native way

So I started to get it from the data API provided by the Windows operating system and wrote it as a Java plug-in

And when it was written, these filtering methods were provided in Java – applicable to all network cards (valid for personal testing) (JDK19 x64)

ip_if_filter: quoted from ipifcons.h

family: quoted from ws2def.h

Can be mixed with the JNAudio text-to-speech plug-in (free x64) I wrote last time

Some data of all network cards obtained by using this plug-in, You can try to use filtering to distinguish wifi and Ethernet

The information, filtering method constant referencedipifcons.h AND ws2def.h can be directly Baidu.

Resource Download

You can choose CSDN/Github resource download (github/csdn)

colorful1.2 Framework

Because these frameworks are separate modular plug-ins integrated in colorful1.2, you need to install the colorful1.2 framework first

Framework installation tutorial/usage tutorial, I will plan to separate some modules of colorful1.2 later and make them into some independent plug-ins

Tutorial

Basic instantiation

 //You need to import the location of this plug-in dll library first
        System.load("Net_Work_Route.dll");
        //Instance Java object
        net_work C=new net_work();
        //C++ binds net_work to the Java instance object
        C.initModel();
        //C.Method
        //After unbinding/releasing/releasing, you will not be able to use any method in net_work and you need to re-initModel.
        C.release_network();

How to use list

<strong>initModel</strong>

Bind C++ methods/classes/other references to Java instantiated objects in the plug-in

You can think of it as the initial method, you need to use it

<strong>release_network (unbinding) release</strong>
<strong>getLocalAdapter</strong> 
Get the names of all local network card adapters. After obtaining them, you can get some information about them based on these names, such as: IP4 address return (List)
<strong>getAdapterAddress</strong>
Parameter String, adapter name, you can use

<strong>ws2def.adapter_null Gets the IP4 return List<String></strong>

of all network card adapter conversations

<strong> getSubnetMask</strong>
Parameter String, adapter name, you can use

<strong>ws2def.adapter_null to get the subnet mask of all network card adapter conversations Code returns List<String></strong>
<strong>getLocalHostIP4</strong>
Parameter int, you need to cooperate

<strong>ip_if_filter.* filters wifi/Ethernet/default gateway/other ip, returns List<String>< for all IPs of all network card adapters for ip_if_filter.NULL /strong>
<strong>getLocalHostMac</strong>
Parameter int, you Need to cooperate

<strong>ip_if_filter.* Filters wifi/Ethernet/default gateway/other MAC, returns List<String></strong>

for all MACs of all network card adapters for ip_if_filter.NULL

<strong>getLocalHostIP6</strong>

Parameter int, you need to match

<strong>ws2def.* Filter wifi/Ethernet/default gateway/other IP6, (all network cards), return List<String></strong>
<strong>getDeviceInfo</strong>
<strong>Host address type: 0
Address List:1
Alias:2
Address length: 3
Formal hostname: 4</strong>

Return List

<strong>getDeviceName</strong>

Returns the name of the current system of the current device

Return String

<strong>release_network</strong>

(Unbinding) Release, you will not be able to use/find the bound Class/function/other references, and call other methods again after use

NULL will be returned and no error will be raised. You need to use initModel to rebind

<strong>getWebStormInfo</strong>

Obtain web host information through the Internet through a valid IP/domain name

<strong>Host address type: 0
Address List:1
Alias:2
Address length: 3
Formal hostname: 4
Return List<String></strong>

After multiple attempts, the other party may reject and return Null

<strong>get_gateway</strong>
Get the current default gateway IP strong>

A simple short try

 System.load("Net_Work_Route.dll path");
        net_work C=new net_work();
        C.initModel();
        List<String> ip_if=C.getAdapterAddress(ws2def.adapter_null);
        console.info(ip_if);
        List<String> device = C.getDeviceInfo();
        console.info("""
                Host address type: %s
                Address list: %s
                Alias: %s
                Address length: %s
                Formal host name: %s""".formatted(device.get(0), device.get(1), device.get(2), device.get(3), device.get(4) ));
        List<String> adapter = C.getLocalAdapter(ip_if_filter.NULL);
        console.info("All network adapters::%s".formatted(adapter.toString()));
        console.info("Current default IP forwarding table default gateway: %s".formatted(C.get_gateway()));
        console.info("The current IP forwarded by all network cards: %s".formatted(C.getLocalHostIP4(ip_if_filter.NULL)));
        console.info("All IPs of all current network card adapters:%s".formatted(C.getAdapterAddress(ws2def.adapter_null)));
        console.info("Current subnet mask of all network card adapters: %s".formatted(C.getSubnetMask(ws2def.adapter_null)));
        console.info("Currently all network card adapter UDP, TCP, etc forwarding objects: %s".formatted(C.getLocalHostIP4(ws2def.AF_INET)));
        C.release_network();