如果通过Java中的DNS无法解析,如何获取本地主机名? [英] How do I get the local hostname if unresolvable through DNS in Java?
问题描述
这听起来像以前应该问过的东西,而且它有一些,但我希望获得机器的本地主机名和IP地址,即使它无法通过DNS解析(在Java中)。
This sounds like something that should have been asked before, and it has sort of, but I'm looking to get the local hostname and IP addresses of a machine even when it is not resolvable through DNS (in Java).
我可以通过迭代 NetworkInterfaces.getNetworkInterfaces()
来获得没有解决方案的本地IP地址。
I can get the local IP addresses without resolution by iterating through NetworkInterfaces.getNetworkInterfaces()
.
我发现这个问题的任何答案都表明要使用getLocalHost()
Any answers to this question I've found indicate to use getLocalHost()
InetAddress localhost = java.net.InetAddress.getLocalHost();
hostName = localhost.getHostName();
但是如果主机名会抛出 UnknownHostException
无法通过DNS解析。
but this throws an UnknownHostException
if the hostname isn't resolvable through DNS.
如果没有在幕后发现DNS查询,是否无法获取本地主机名?
Is there no way to get the local hostname without a DNS lookup happening behind the scenes?
编辑:检索到的IP地址是10.4.168.23
例外是 java.net.UnknownHostException:cms1.companyname.com:cms1.companyname.com
(主机名已更改为伪匿名),并且hosts文件不包含主机名。但它知道它的主机名,所以我不确定为什么我不能在没有抛出异常的情况下得到它。
edit: the IP address retrieved is 10.4.168.23
The exception is java.net.UnknownHostException: cms1.companyname.com: cms1.companyname.com
(hostname changed for pseudo-anonymity), and the hosts file does not contain the hostname. But it does know its hostname, so I'm not sure why I can't get it without an exception being thrown.
推荐答案
是的, 应该在Java中获取主机名而不使用名称服务查找,但遗憾的是没有。
Yes, there should be a way in Java to get the hostname without resorting to name service lookups but unfortunately there isn't.
但是,你可以这样做:
if (System.getProperty("os.name").startsWith("Windows")) {
// Windows will always set the 'COMPUTERNAME' variable
return System.getenv("COMPUTERNAME");
} else {
// If it is not Windows then it is most likely a Unix-like operating system
// such as Solaris, AIX, HP-UX, Linux or MacOS.
// Most modern shells (such as Bash or derivatives) sets the
// HOSTNAME variable so lets try that first.
String hostname = System.getenv("HOSTNAME");
if (hostname != null) {
return hostname;
} else {
// If the above returns null *and* the OS is Unix-like
// then you can try an exec() and read the output from the
// 'hostname' command which exist on all types of Unix/Linux.
// If you are an OS other than Unix/Linux then you would have
// to do something else. For example on OpenVMS you would find
// it like this from the shell: F$GETSYI("NODENAME")
// which you would probably also have to find from within Java
// via an exec() call.
// If you are on zOS then who knows ??
// etc, etc
}
}
<这将使您在传统的Sun JDK平台(Windows,Solaris,Linux)上获得100%的所需,但如果您的操作系统更加激动(从Java角度来看),则会变得不那么容易。请参阅代码示例中的注释。
and that will get you 100% what you want on the traditional Sun JDK platforms (Windows, Solaris, Linux) but becomes less easy if your OS is more excotic (from a Java perspective). See the comments in the code example.
我希望有更好的方法。
这篇关于如果通过Java中的DNS无法解析,如何获取本地主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!