简体   繁体   English

在 Java 中检查字符串是主机名还是 IP 地址

[英]Check if a string is a hostname or an ip-address in Java

I want to check if a string is a hostname or an ip-address in Java.我想检查字符串是 Java 中的主机名还是 IP 地址。 Is there an API to do it or must I write a parser myself?是否有 API 可以做到这一点,还是我必须自己编写解析器?

The problem is complex because there are IPv4 addresses, short and long IPv6 addresses, short hostnames and FQDN host names.这个问题很复杂,因为有 IPv4 地址、短和长 IPv6 地址、短主机名和 FQDN 主机名。

There doesn't appear to be an API, but writing such function doesn't seem to be very difficult.貌似没有API,但是写这样的函数好像不是很困难。 You can check the following conditions in your code:您可以在代码中检查以下条件:

  1. If sourceStr contains ":" but no "."如果 sourceStr 包含 ":" 但没有 "." -> IPv6 -> IPv6
  2. If sourceStr.matches("^.[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}") == true) -> IPv4如果 sourceStr.matches("^.[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}/..[0- 9]{1,3}") == true) -> IPv4
  3. If sourceStr contains "."如果 sourceStr 包含“.” -> FQDN host name -> FQDN 主机名
  4. Otherwise it must be a short hostname否则它必须是一个短主机名

To expand @MrGomez 's answer, Use InetAddresses to validate IP Addresses and InternetDomainName to validate hostnames (FQDN).要扩展@MrGomez 的答案,请使用InetAddresses验证 IP 地址和InternetDomainName验证主机名 (FQDN)。

Example:例子:

public static boolean validate(final String hostname) {
        return InetAddresses.isUriInetAddress(hostname) || InternetDomainName.isValid(hostname);
    }

在 JDK API 中不可能,但您可以在google guava 中使用InetAddresses.forString当参数不是 IP 字符串文字时抛出异常。

While you could theoretically write rules to handle these cases yourself, using the usual cadre of RFCs , I'd instead look at the entirety of this class in Google Guava , especially the corner cases, such as how it resolves the embedding of 4-in-6 addresses.虽然理论上你就可以编写规则来自己处理这些情况下,使用通常干部RFC文档,我不是看这个类在谷歌番石榴的整体,尤其是极端案例,比如它是如何解决的4-在嵌入-6 个地址。

As for determining if you have a FQDN, see if coercion to IP address fails, then try to resolve it against DNS.至于确定你是否有FQDN,看看强制IP地址是否失败,然后尝试针对DNS进行解析。 Anything else should, given your input cases, be a hostname or local resolution that isn't fully qualified.考虑到您的输入情况,其他任何内容都应该是不完全限定的主机名或本地解析。

The IPAddress Java library will do it. IPAddress Java 库会做到这一点。 The javadoc is available at the link.链接中提供了 javadoc。 Disclaimer: I am the project manager.免责声明:我是项目经理。

static void check(HostName host) {
    try {
        host.validate();
        if(host.isAddress()) {
            System.out.println("address: " + host.asAddress());
        } else {
            System.out.println("host name: " + host);
        }
    } catch(HostNameException e) {
        System.out.println(e.getMessage());
    }
}

public static void main(String[] args) {
    HostName host = new HostName("1.2.3.4");
    check(host);
    host = new HostName("1.2.a.4");
    check(host);
    host = new HostName("::1");
    check(host);
    host = new HostName("[::1]");
    check(host);
    host = new HostName("1.2.?.4");
    check(host);  
}

Output:输出:

address: 1.2.3.4
host name: 1.2.a.4
address: ::1
address: ::1
1.2.?.4 Host error: invalid character at index 4
import sun.net.util.IPAddressUtil
IPAddressUtil.isIPv4LiteralAddress(addr)
IPAddressUtil.isIPv6LiteralAddress(addr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM