Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set Default Gateway,Ip Address and Subnet mask from Java?

I am looking some way to configure my Ethernet card from Java. Is there any way to change Default Gateway,IP address and subnet mask from Java.

Currently I am using OSHIto get the ip address,Mac address and other Hardware info. I also understand that the only way to set these parameters is Java.lang.Runtime Class and using Hardware specific command to set properties from here

I am looking for a cleaner way to do this for Linux, Windows and Mac. I am basically trying to set all this properties from my own application. Is there a library or wrapper like OSHI to do execute host specific command?

like image 326
Rengas Avatar asked Oct 05 '16 10:10

Rengas


1 Answers

You can do something like this

String str1="192.168.0.201";
String str2="255.255.255.0";
String[] command1 = { "netsh", "interface", "ip", "set", "address",
"name=", "Local Area Connection" ,"source=static", "addr=",str1,
"mask=", str2};
Process pp = java.lang.Runtime.getRuntime().exec(command1);
like image 62
Andreas Mattisson Avatar answered Oct 03 '22 02:10

Andreas Mattisson