001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package examples.cidr; 018 019import java.util.Arrays; 020import java.util.Scanner; 021 022import org.apache.commons.net.util.SubnetUtils; 023import org.apache.commons.net.util.SubnetUtils.SubnetInfo; 024 025/** 026 * Example class that shows how to use the {@link SubnetUtils} class. 027 * 028 */ 029public class SubnetUtilsExample { 030 031 public static void main(String[] args) { 032 String subnet = "192.168.0.3/31"; 033 SubnetUtils utils = new SubnetUtils(subnet); 034 SubnetInfo info = utils.getInfo(); 035 036 System.out.printf("Subnet Information for %s:\n", subnet); 037 System.out.println("--------------------------------------"); 038 System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(), 039 Integer.toBinaryString(info.asInteger(info.getAddress()))); 040 System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(), 041 Integer.toBinaryString(info.asInteger(info.getNetmask()))); 042 System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature()); 043 044 System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress()); 045 046 System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(), 047 Integer.toBinaryString(info.asInteger(info.getNetworkAddress()))); 048 System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(), 049 Integer.toBinaryString(info.asInteger(info.getBroadcastAddress()))); 050 System.out.printf("Low Address:\t\t\t%s\t[%s]\n", info.getLowAddress(), 051 Integer.toBinaryString(info.asInteger(info.getLowAddress()))); 052 System.out.printf("High Address:\t\t\t%s\t[%s]\n", info.getHighAddress(), 053 Integer.toBinaryString(info.asInteger(info.getHighAddress()))); 054 055 System.out.printf("Total usable addresses: \t%d\n", Long.valueOf(info.getAddressCountLong())); 056 System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses())); 057 058 final String prompt ="Enter an IP address (e.g. 192.168.0.10):"; 059 System.out.println(prompt); 060 Scanner scanner = new Scanner(System.in); 061 while (scanner.hasNextLine()) { 062 String address = scanner.nextLine(); 063 System.out.println("The IP address [" + address + "] is " 064 + (info.isInRange(address) ? "" : "not ") 065 + "within the subnet [" + subnet + "]"); 066 System.out.println(prompt); 067 } 068 scanner.close(); 069 } 070 071}