Getting Network Information in Java on Windows and Linux

I need network configuration information in my application, which is more than java.net.NetworkInterface and java.net.InetAddress . I would like to include information such as Subnet, Gateway, and DNS servers, but the code must be portable on both Windows and Linux platforms.

I learned this by doing Runtime.getRuntime().exec("ipconfig") for Windows and Runtime.getRuntime().exec("ifconfig") for Linux and analyzing the results. Perhaps there is a better way to do this or an open source project that I could use.

Thank you in advance for any ideas that can guide me in the right direction.

+6
source share
2 answers

If you are thinking of a more efficient and reliable solution, perhaps you can use the idea of ​​SNMP (Simple Network Management Protocol). There are several APIs available for use, SNMP4J is the most famous and open source. Since SNMP is supported across several operating systems, and even switches and routers, it is probably a good idea to use it to get detailed network-level statistics. But if you want a simpler solution, use the Sigar API, which supports many operating systems, including windows and linux. One problem with Sigar is that it is licensed under the GPL, which prevents you from deploying it in a commercial project.

+1
source

You can do it in your own code and use JNI. Then send the .dll for Windows and the .so (or source code) for linux, although this is probably not much better than the "exec" solution.

0
source

Source: https://habr.com/ru/post/901568/


All Articles