获取java中的默认网关
我想使用java获取本地计算机的默认网关。我知道如何通过执行dos或shell命令来获取它,但是还有其他方法获取它吗?获取java中的默认网关,java,networking,dns,ip,gateway,Java,Networking,Dns,Ip,Gateway,我想使用java获取本地计算机的默认网关。我知道如何通过执行dos或shell命令来获取它,但是还有其他方法获取它吗? 还需要获取主dns ip和辅助dns ip。这不是一个简单的方法。您必须调用本地系统命令并解析输出,或者读取配置文件或注册表。据我所知,没有一种平台独立的方式可以实现这一点——如果你想在linux、mac和windows上运行,就必须为它们编写代码 看 这涵盖了网关,您也可以使用ifconfig或ipconfig来实现这一点。对于DNS信息,您必须调用不同的系统命令,如Wind
还需要获取主dns ip和辅助dns ip。这不是一个简单的方法。您必须调用本地系统命令并解析输出,或者读取配置文件或注册表。据我所知,没有一种平台独立的方式可以实现这一点——如果你想在linux、mac和windows上运行,就必须为它们编写代码 看
这涵盖了网关,您也可以使用ifconfig或ipconfig来实现这一点。对于DNS信息,您必须调用不同的系统命令,如Windows上的ipconfig或Linux或mac上的parse/etc/resolv.conf。目前Java中没有标准接口来获取默认网关或DNS服务器地址。您需要一个shell命令。是: 由于使用了数据报(UDP),它没有连接到任何地方,因此端口号可能没有意义,远程地址(1.1.1.1)不需要访问,只需路由即可。
在Windows中,借助于
ipconfig
:
在这里,我获取路由器的默认网关IP地址,并在浏览器中打开它以查看路由器的设置页面。
我不确定它是否适用于所有系统,但至少在这里我发现:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main
{
public static void main(String[] args)
{
try
{
//Variables to find out the Default Gateway IP(s)
String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
String hostName = InetAddress.getLocalHost().getHostName();
//"subtract" the hostName from the canonicalHostName, +1 due to the "." in there
String defaultGatewayLeftover = canonicalHostName.substring(hostName.length() + 1);
//Info printouts
System.out.println("Info:\nCanonical Host Name: " + canonicalHostName + "\nHost Name: " + hostName + "\nDefault Gateway Leftover: " + defaultGatewayLeftover + "\n");
System.out.println("Default Gateway Addresses:\n" + printAddresses(InetAddress.getAllByName(defaultGatewayLeftover)));
} catch (UnknownHostException e)
{
e.printStackTrace();
}
}
//simple combined string out the address array
private static String printAddresses(InetAddress[] allByName)
{
if (allByName.length == 0)
{
return "";
} else
{
String str = "";
int i = 0;
while (i < allByName.length - 1)
{
str += allByName[i] + "\n";
i++;
}
return str + allByName[i];
}
}
}
这并不总是返回默认网关,它是完全组成的。包路由是由多个因素决定的,其中一个因素是目的地地址。如果您的默认网关无法接受/路由您的目的地地址,您的数据包将通过另一条路由发送,这可能是或可能不是所有可用路由中最差的;根据您的网络适配器指标,它也可能是次优路由或介于两者之间的路由。请不要编写带有假设的软件,这会使整个业务情况变得更糟。这是相当巧妙的。不过,它确实取决于目的地地址;最好使用,以确保您至少获得了一个有效的路由,即使它不是默认路由
a.root-servers.net
保证始终存在,并且不可能存在于您的本地网络中。不幸的是,这对我不起作用。只是给我本地主机。我想我只要做getHostAddress
。获取前3个八位组,并假设xxx.xxx.xxx.1或xxx.xxx.xxx.254是网关。
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();
}
}
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main
{
public static void main(String[] args)
{
try
{
//Variables to find out the Default Gateway IP(s)
String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
String hostName = InetAddress.getLocalHost().getHostName();
//"subtract" the hostName from the canonicalHostName, +1 due to the "." in there
String defaultGatewayLeftover = canonicalHostName.substring(hostName.length() + 1);
//Info printouts
System.out.println("Info:\nCanonical Host Name: " + canonicalHostName + "\nHost Name: " + hostName + "\nDefault Gateway Leftover: " + defaultGatewayLeftover + "\n");
System.out.println("Default Gateway Addresses:\n" + printAddresses(InetAddress.getAllByName(defaultGatewayLeftover)));
} catch (UnknownHostException e)
{
e.printStackTrace();
}
}
//simple combined string out the address array
private static String printAddresses(InetAddress[] allByName)
{
if (allByName.length == 0)
{
return "";
} else
{
String str = "";
int i = 0;
while (i < allByName.length - 1)
{
str += allByName[i] + "\n";
i++;
}
return str + allByName[i];
}
}
}
Info:
Canonical Host Name: PCK4D-PC.speedport.ip
Host Name: PCK4D-PC
Default Gateway Leftover: speedport.ip
Default Gateway Addresses:
speedport.ip/192.168.2.1
speedport.ip/fe80:0:0:0:0:0:0:1%12