Example usage for org.apache.commons.net.util SubnetUtils SubnetUtils

List of usage examples for org.apache.commons.net.util SubnetUtils SubnetUtils

Introduction

In this page you can find the example usage for org.apache.commons.net.util SubnetUtils SubnetUtils.

Prototype

public SubnetUtils(String cidrNotation) 

Source Link

Document

Constructor that takes a CIDR-notation string, e.g.

Usage

From source file:examples.SubnetUtilsExample.java

public static void main(String[] args) {
    String subnet = "192.168.0.1/29";
    SubnetUtils utils = new SubnetUtils(subnet);
    SubnetInfo info = utils.getInfo();/*  w  w w.j  av a2  s. c  o m*/

    System.out.printf("Subnet Information for %s:\n", subnet);
    System.out.println("--------------------------------------");
    System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(),
            Integer.toBinaryString(info.asInteger(info.getAddress())));
    System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(),
            Integer.toBinaryString(info.asInteger(info.getNetmask())));
    System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature());

    System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress());

    System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(),
            Integer.toBinaryString(info.asInteger(info.getNetworkAddress())));
    System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(),
            Integer.toBinaryString(info.asInteger(info.getBroadcastAddress())));
    System.out.printf("First Usable Address:\t\t%s\t[%s]\n", info.getLowAddress(),
            Integer.toBinaryString(info.asInteger(info.getLowAddress())));
    System.out.printf("Last Usable Address:\t\t%s\t[%s]\n", info.getHighAddress(),
            Integer.toBinaryString(info.asInteger(info.getHighAddress())));

    System.out.printf("Total usable addresses: \t%d\n", info.getAddressCount());
    System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses()));

    final String prompt = "Enter an IP address (e.g. 192.168.0.10):";
    System.out.println(prompt);
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextLine()) {
        String address = scanner.nextLine();
        System.out.println("The IP address [" + address + "] is " + (info.isInRange(address) ? "" : "not ")
                + "within the subnet [" + subnet + "]");
        System.out.println(prompt);
    }

}

From source file:examples.cidr.SubnetUtilsExample.java

public static void main(String[] args) {
    String subnet = "192.168.0.3/31";
    SubnetUtils utils = new SubnetUtils(subnet);
    SubnetInfo info = utils.getInfo();/*from  ww w  . j  ava  2  s  .  com*/

    System.out.printf("Subnet Information for %s:\n", subnet);
    System.out.println("--------------------------------------");
    System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(),
            Integer.toBinaryString(info.asInteger(info.getAddress())));
    System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(),
            Integer.toBinaryString(info.asInteger(info.getNetmask())));
    System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature());

    System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress());

    System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(),
            Integer.toBinaryString(info.asInteger(info.getNetworkAddress())));
    System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(),
            Integer.toBinaryString(info.asInteger(info.getBroadcastAddress())));
    System.out.printf("Low Address:\t\t\t%s\t[%s]\n", info.getLowAddress(),
            Integer.toBinaryString(info.asInteger(info.getLowAddress())));
    System.out.printf("High Address:\t\t\t%s\t[%s]\n", info.getHighAddress(),
            Integer.toBinaryString(info.asInteger(info.getHighAddress())));

    System.out.printf("Total usable addresses: \t%d\n", Integer.valueOf(info.getAddressCount()));
    System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses()));

    final String prompt = "Enter an IP address (e.g. 192.168.0.10):";
    System.out.println(prompt);
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextLine()) {
        String address = scanner.nextLine();
        System.out.println("The IP address [" + address + "] is " + (info.isInRange(address) ? "" : "not ")
                + "within the subnet [" + subnet + "]");
        System.out.println(prompt);
    }

}

From source file:com.jaeksoft.searchlib.util.NetworksUtils.java

public final static SubnetInfo[] getSubnetArray(String ips) throws IOException {
    if (ips == null)
        return null;
    StringReader sr = new StringReader(ips);
    try {//w  w  w  .  j  a v a  2  s . c  o  m
        List<String> lines = IOUtils.readLines(sr);
        if (lines == null)
            return null;
        SubnetInfo[] subnetArray = new SubnetInfo[lines.size()];
        int i = 0;
        for (String line : lines) {
            line = line.trim();
            if (line.isEmpty())
                continue;
            if (line.indexOf('/') == -1)
                line = line + "/32";
            SubnetUtils subnetUtils = new SubnetUtils(line);
            subnetUtils.setInclusiveHostCount(true);
            subnetArray[i++] = subnetUtils.getInfo();
        }
        return subnetArray;
    } finally {
        IOUtils.closeQuietly(sr);
    }
}

From source file:com.unisa.hive.udf.getHighestIP.java

public Text evaluate(Text inputaddr) throws UnknownHostException {

    String high_ip;//from w  ww. j  av a  2  s. c  o  m
    Text ip_out;

    SubnetUtils utils = new SubnetUtils(inputaddr.toString());
    high_ip = utils.getInfo().getHighAddress();

    ip_out = new Text(high_ip);

    return ip_out;
}

From source file:com.unisa.hive.udf.getLowestIP.java

public Text evaluate(Text inputaddr) throws UnknownHostException {

    String low_ip;// w ww .  java 2s.c  om
    Text ip_out;

    SubnetUtils utils = new SubnetUtils(inputaddr.toString());
    low_ip = utils.getInfo().getLowAddress();

    ip_out = new Text(low_ip);

    return ip_out;
}

From source file:io.sip3.tapir.salto.Hosts.java

@Autowired(required = false)
public Hosts(HostConfiguration configuration) {
    configuration.getHosts().forEach(host -> {
        String name = host.getName();
        host.getAddr().forEach(addr -> {
            if (addr.contains("/")) {
                Arrays.asList(new SubnetUtils(addr).getInfo().getAllAddresses())
                        .forEach(a -> hosts.put(a, name));
            } else {
                hosts.put(addr, name);/*from w  w w  . ja va  2  s. c  o m*/
            }
        });
    });
}

From source file:de.yaio.commons.net.IpAddressMatcher.java

/** 
 * creates an IpAddressMatcher/*from w  w w .  j  ava  2s.  c om*/
 * @param ipList                 list of ip-addresses with/without netmask
 */
public IpAddressMatcher(List<String> ipList) {
    this.ipList = new ArrayList<SubnetUtils>();
    for (String ip : ipList) {
        if (StringUtils.isBlank(ip)) {
            continue;
        }
        SubnetUtils ipMatcher;
        if (ip.indexOf("/") > 0) {
            ipMatcher = new SubnetUtils(ip);
        } else {
            ipMatcher = new SubnetUtils(ip + "/32");
        }
        ipMatcher.setInclusiveHostCount(true);
        this.ipList.add(ipMatcher);
    }
}

From source file:de.uni_bonn.bit.IPAddressHelper.java

/**
 * Returns an IP address of this machine that is in the same subnet as one of the peerAddresses.
 * @param peerAddresses List of peer addresses in CIDR notation
 * @return Ip address as string without subnet information
 *///w  w  w .  j a va2s.  c  om
public static String findFirstAddressInCommonNetwork(List<String> peerAddresses) {
    List<String> myAddresses = getAllUsableIPAddresses();
    for (String myAdress : myAddresses) {
        SubnetUtils.SubnetInfo subnetInfo = new SubnetUtils(myAdress).getInfo();
        for (String peerAddress : peerAddresses) {
            String peerAddressWithoutSubnet = new SubnetUtils(peerAddress).getInfo().getAddress();
            if (subnetInfo.isInRange(peerAddressWithoutSubnet))
                return peerAddressWithoutSubnet;
        }

    }
    return "";
}

From source file:com.binarybirchtree.filters.IpFilter.java

public static boolean ipIsInList(String ip, String[] list) {
    if (ip == null)
        return false;

    int failed = 0;
    for (String listIp : list) {
        SubnetUtils subnet = new SubnetUtils(listIp);
        subnet.setInclusiveHostCount(true);

        if (!subnet.getInfo().isInRange(ip))
            failed++;/*from   w  ww .  j av a  2  s  .co m*/
    }

    return failed < list.length;
}

From source file:it.jugtorino.one.msvc.way.rabbit.geoip.reference.service.IPLocation.java

private Predicate<Map<String, Object>> filterOutNonInRange(String ipAddress) {
    return network -> new SubnetUtils(network.get("network").toString()).getInfo().isInRange(ipAddress);
}