Get default gateway in java
Solution 1
There is not an easy way to do this. You'll have to call local system commands and parse the output, or read configuration files or the registry. There is no platform independent way that I'm aware of to make this work - you'll have to code for linux, mac and windows if you want to run on all of them.
See How can I determine the IP of my router/gateway in Java?
That covers the gateway, and you could use ifconfig or ipconfig as well to get this. For DNS info, you'll have to call a different system command such as ipconfig on Windows or parse /etc/resolv.conf on Linux or mac.
Solution 2
My way is:
try(DatagramSocket s=new DatagramSocket())
{
s.connect(InetAddress.getByAddress(new byte[]{1,1,1,1}), 0);
return NetworkInterface.getByInetAddress(s.getLocalAddress()).getHardwareAddress();
}
Because of using datagram (UDP), it isn't connecting anywhere, so port number may be meaningless and remote address (1.1.1.1) needn't be reachable, just routable.
Solution 3
In Windows with the help of ipconfig
:
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
public final class Router {
private static final String DEFAULT_GATEWAY = "Default Gateway";
private Router() {
}
public static void main(String[] args) {
if (Desktop.isDesktopSupported()) {
try {
Process process = Runtime.getRuntime().exec("ipconfig");
try (BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.trim().startsWith(DEFAULT_GATEWAY)) {
String ipAddress = line.substring(line.indexOf(":") + 1).trim(),
routerURL = String.format("http://%s", ipAddress);
// opening router setup in browser
Desktop.getDesktop().browse(new URI(routerURL));
}
System.out.println(line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Here I'm getting the default gateway IP address of my router, and opening it in a browser to see my router's setup page.
Nirmal- thInk beYond
Updated on June 28, 2022Comments
-
Nirmal- thInk beYond almost 2 years
I want to fetch default gateway for local machine using java. I know how to get it by executing dos or shell commands, but is there any another way to fetch? Also need to fetch primary and secondary dns ip.
-
specializt almost 9 yearsthis will not always return the default gateway, thats completely made up. Packet routing is determined via multiple factors, one of them is the destination address. If your default gateway cannot accept / route your destination address your packet will be delivered via another route, which may or may not be the worst of all of the available routes; depending on your network adapter metrics it may also be the second best route or something in between. Please dont write software with assumptions, that makes the whole business situation worse.
-
Tom Anderson about 7 yearsThis is pretty ingenious. It does depend on the destination address, though; it might be better to use a real address that's out there on the internet to ensure that you're at least getting a valid route out, even if it's not the default route.
-
Tom Anderson about 7 yearsIf you have working DNS, you can use a hostname instead of an address here;
a.root-servers.net
is guaranteed to always exist, and is unlikely to be in your local network. -
MDuh almost 5 yearsthis didn't work for me unfortunately. Just giving me localhost. I think I'll just do
getHostAddress
. Get the first 3 octet and assume that either xxx.xxx.xxx.1 or xxx.xxx.xxx.254 is the gateway.