View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package examples.cidr;
18  
19  import java.util.Arrays;
20  import java.util.Scanner;
21  
22  import org.apache.commons.net.util.SubnetUtils;
23  import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
24  
25  /**
26   * Example class that shows how to use the {@link SubnetUtils} class.
27   *
28   */
29  public class SubnetUtilsExample {
30  
31      public static void main(String[] args) {
32          String subnet = "192.168.0.3/31";
33          SubnetUtils utils = new SubnetUtils(subnet);
34          SubnetInfo info = utils.getInfo();
35  
36          System.out.printf("Subnet Information for %s:\n", subnet);
37          System.out.println("--------------------------------------");
38          System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(),
39                  Integer.toBinaryString(info.asInteger(info.getAddress())));
40          System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(),
41                  Integer.toBinaryString(info.asInteger(info.getNetmask())));
42          System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature());
43  
44          System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress());
45  
46          System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(),
47                  Integer.toBinaryString(info.asInteger(info.getNetworkAddress())));
48          System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(),
49                  Integer.toBinaryString(info.asInteger(info.getBroadcastAddress())));
50          System.out.printf("Low Address:\t\t\t%s\t[%s]\n", info.getLowAddress(),
51                  Integer.toBinaryString(info.asInteger(info.getLowAddress())));
52          System.out.printf("High Address:\t\t\t%s\t[%s]\n", info.getHighAddress(),
53                  Integer.toBinaryString(info.asInteger(info.getHighAddress())));
54  
55          System.out.printf("Total usable addresses: \t%d\n", Long.valueOf(info.getAddressCountLong()));
56          System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses()));
57  
58          final String prompt ="Enter an IP address (e.g. 192.168.0.10):";
59          System.out.println(prompt);
60          Scanner scanner = new Scanner(System.in);
61          while (scanner.hasNextLine()) {
62              String address = scanner.nextLine();
63              System.out.println("The IP address [" + address + "] is "
64                      + (info.isInRange(address) ? "" : "not ")
65                      + "within the subnet [" + subnet + "]");
66              System.out.println(prompt);
67          }
68          scanner.close();
69      }
70  
71  }