Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java check if IPv4 or IPv6 address is in a given subnet [closed]

How can I check if an IP address is in a given subnet? I was able to do this by using Apache Commons SubnetUtils (SubnetUtils.SubnetInfo.isInRange) but it does not support IPv6 yet.

like image 571
Jan H Avatar asked Jun 10 '13 13:06

Jan H


People also ask

How do you check IP address is IPv4 or IPv6 Java?

We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address. isValid(inetAddress) : Returns true if the specified string is a valid IPv4 or IPv6 address. isValidInet4Address(inet4Address) : Returns true if the specified string is a valid IPv4 address.

How do you identify the IPv4 address and IPv6 address in the given string?

Given a string S consisting of N characters, the task is to check if the given string S is IPv4 or IPv6 or Invalid. If the given string S is a valid IPv4, then print “IPv4”, if the string S is a valid IPv6, then print “IPv4”. Otherwise, print “-1”. A valid IPv4 address is an IP in the form “x1.


1 Answers

edazdarevic's CIDRUtils supports both IPv4 and IPv6. The example does not mention boolean isInRange(String ipAddress), but it is implemented!

Another option is java-ipv6, but it does not support IPv4 and requires JDK7.

like image 185
Jan H Avatar answered Oct 14 '22 19:10

Jan H