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 */ 017 package examples; 018 019 import java.util.Arrays; 020 import java.util.Scanner; 021 022 import org.apache.commons.net.util.SubnetUtils; 023 import org.apache.commons.net.util.SubnetUtils.SubnetInfo; 024 025 /** 026 * Example class that shows how to use the {@link SubnetUtils} class. 027 * @author Rory Winston <rwinston@apache.org> 028 * 029 */ 030 public class SubnetUtilsExample { 031 032 public static void main(String[] args) { 033 String subnet = "192.168.0.1/29"; 034 SubnetUtils utils = new SubnetUtils(subnet); 035 SubnetInfo info = utils.getInfo(); 036 037 System.out.printf("Subnet Information for %s:\n", subnet); 038 System.out.println("--------------------------------------"); 039 System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(), 040 Integer.toBinaryString(info.asInteger(info.getAddress()))); 041 System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(), 042 Integer.toBinaryString(info.asInteger(info.getNetmask()))); 043 System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature()); 044 045 System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress()); 046 047 System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(), 048 Integer.toBinaryString(info.asInteger(info.getNetworkAddress()))); 049 System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(), 050 Integer.toBinaryString(info.asInteger(info.getBroadcastAddress()))); 051 System.out.printf("First Usable Address:\t\t%s\t[%s]\n", info.getLowAddress(), 052 Integer.toBinaryString(info.asInteger(info.getLowAddress()))); 053 System.out.printf("Last Usable Address:\t\t%s\t[%s]\n", info.getHighAddress(), 054 Integer.toBinaryString(info.asInteger(info.getHighAddress()))); 055 056 System.out.printf("Total usable addresses: \t%d\n", info.getAddressCount()); 057 System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses())); 058 059 final String prompt ="Enter an IP address (e.g. 192.168.0.10):"; 060 System.out.println(prompt); 061 Scanner scanner = new Scanner(System.in); 062 while (scanner.hasNextLine()) { 063 String address = scanner.nextLine(); 064 System.out.println("The IP address [" + address + "] is " 065 + (info.isInRange(address) ? "" : "not ") 066 + "within the subnet [" + subnet + "]"); 067 System.out.println(prompt); 068 } 069 070 } 071 072 }