How do you determine if an IP string or hostname is

So, you have a String that is retrieved from the admin user interface (so this is definitely a string). How to find out if this string is an IP address or host name in Java?

Update: I think I didn’t understand, I ask more if there is anything in the Java SDK that I can use to distinguish between IP addresses and host names? Sorry for the confusion and thanks to everyone who took / will take the time to answer this question.

+4
source share
8 answers

You can use regex with this pattern:

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b 

This will tell you if it is an IPv4 address.

+14
source

You can see if the string matches the format of number.number.number.number, for example:

 \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b 

will match any of 0 - 999 .

Everything that you can use by default for a host name.

+2
source

Do we have an assumption that this is one or the other, and not something completely different? If so, I would probably use a regex to see if it matches the dotted quad format.

+2
source
 URI validator = new URI(yourString); 

This code will check the IP address or host name. (It throws an invalid URI exception if the string is invalid)

If you are trying to distinguish between the two ... then I will skip your question.

+1
source

Could you just combine regular expression with it?

0
source

You can use the security manager with a call to InetAddress.getByName(addr) .

If addr is not a dotted square, getByName will try to make a connection to search by name, which the security manager can capture as a call to checkConnect(addr, -1) , resulting in a SecurityException that you can catch.

You can use System.setSecurityManager() if you are doing a fully privileged nesting of your custom security manager before calling getByName .

0
source

It's not as simple as it might seem, there are some ambiguities around characters, such as hyphens, underscores, and square brackets '-', '_', '[]'.

The Java SDK has some limitations in this area. When using InetAddress.getByName, it will go online to resolve DNS names and resolve an address that is expensive and unnecessary if all you want is to determine the host address and address. In addition, if the address is written in a slightly different but valid format (common in IPv6), then string comparisons based on the results of InetAddress.getByName will not work.

The Java IPAddress library will do this. Javadoc is available here. 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 
0
source

Use InetAddress # getAllByName (String hostOrIp) - if hostOrIp is an IP address, the result is an array with one InetAddress, and it .getHostAddress() returns the same string as hostOrIp .

 import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; public class IPvsHostTest { private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(IPvsHostTest.class); @org.junit.Test public void checkHostValidity() { Arrays.asList("10.10.10.10", "google.com").forEach( hostname -> isHost(hostname)); } private void isHost(String ip){ try { InetAddress[] ips = InetAddress.getAllByName(ip); LOG.info("IP-addresses for {}", ip); Arrays.asList(ips).forEach( ia -> { LOG.info(ia.getHostAddress()); }); } catch (UnknownHostException e) { LOG.error("Invalid hostname", e); } } } 

Output:

 IP-addresses for 10.10.10.10 10.10.10.10 IP-addresses for google.com 64.233.164.100 64.233.164.138 64.233.164.139 64.233.164.113 64.233.164.102 64.233.164.101 
0
source

Source: https://habr.com/ru/post/1276714/


All Articles