1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.examples.cidr;
18
19 import java.nio.charset.Charset;
20 import java.util.Scanner;
21
22 import org.apache.commons.net.util.SubnetUtils6;
23 import org.apache.commons.net.util.SubnetUtils6.SubnetInfo;
24
25
26
27
28 public class SubnetUtils6Example {
29
30 public static void main(final String[] args) {
31 final String subnet = "2001:db8:85a3::8a2e:370:7334/64";
32 final SubnetUtils6 utils = new SubnetUtils6(subnet);
33 final SubnetInfo info = utils.getInfo();
34
35 System.out.printf("Subnet Information for %s:%n", subnet);
36 System.out.println("--------------------------------------");
37 System.out.printf("IP Address:\t\t\t%s%n", info.getAddress());
38 System.out.printf("Prefix Length:\t\t\t%d%n", info.getPrefixLength());
39 System.out.printf("CIDR Representation:\t\t%s%n%n", info.getCidrSignature());
40
41 System.out.printf("Network Address:\t\t%s%n", info.getNetworkAddress());
42 System.out.printf("Low Address:\t\t\t%s%n", info.getLowAddress());
43 System.out.printf("High Address:\t\t\t%s%n", info.getHighAddress());
44
45 System.out.printf("Total addresses in subnet:\t%s%n%n", info.getAddressCount());
46
47 final String prompt = "Enter an IPv6 address (e.g., 2001:db8:85a3::1):";
48 System.out.println(prompt);
49 try (Scanner scanner = new Scanner(System.in, Charset.defaultCharset().name())) {
50 while (scanner.hasNextLine()) {
51 final String address = scanner.nextLine();
52 System.out.println("The IP address [" + address + "] is " + (info.isInRange(address) ? "" : "not ") + "within the subnet [" + subnet + "]");
53 System.out.println(prompt);
54 }
55 }
56 }
57
58 }