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

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

Introduction

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

Prototype

public final SubnetInfo getInfo() 

Source Link

Document

Return a SubnetInfo instance that contains subnet-specific statistics

Usage

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();

    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);//w  w w  .  j ava 2s .  c o m
    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.SubnetUtilsExample.java

public static void main(String[] args) {
    String subnet = "192.168.0.1/29";
    SubnetUtils utils = new SubnetUtils(subnet);
    SubnetInfo info = utils.getInfo();

    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);//from w  ww . j  a  v  a  2  s  .co  m
    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 {/*from  w  ww .  j  a va 2s . 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.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  w w . j a va 2s .co  m*/
    }

    return failed < list.length;
}

From source file:com.vmware.photon.controller.cloudstore.xenon.entity.DhcpSubnetServiceTest.java

private static DhcpSubnetService.State createInitialState() {
    SubnetUtils subnetUtils = new SubnetUtils("192.168.0.0/16");
    SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
    Long lowIp, highIp;// w w w . ja  va  2  s.c om

    InetAddress lowIpAddress = InetAddresses.forString(subnetInfo.getLowAddress());
    lowIp = IpHelper.ipToLong((Inet4Address) lowIpAddress);

    InetAddress highIpAddress = InetAddresses.forString(subnetInfo.getHighAddress());
    highIp = IpHelper.ipToLong((Inet4Address) highIpAddress);

    DhcpSubnetService.State startState = new DhcpSubnetService.State();
    startState.lowIp = lowIp;
    startState.highIp = highIp;

    return startState;
}

From source file:com.vmware.photon.controller.api.backend.workflows.AssignFloatingIpToVmWorkflowServiceTest.java

private static DhcpSubnetService.State createDhcpRootSubnetServiceInCloudStore(TestEnvironment testEnvironment)
        throws Throwable {
    String cidr = "192.168.1.0/24";
    SubnetUtils subnetUtils = new SubnetUtils(cidr);
    SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();

    DhcpSubnetService.State state = new DhcpSubnetService.State();
    state.cidr = cidr;/*from   w ww  .  j  av  a2  s  .c  om*/
    state.lowIp = IpHelper.ipStringToLong(subnetInfo.getLowAddress());
    state.highIp = IpHelper.ipStringToLong(subnetInfo.getHighAddress());
    state.isFloatingIpSubnet = true;
    state.documentSelfLink = DhcpSubnetService.FLOATING_IP_SUBNET_SINGLETON_LINK;
    state.subnetId = ServiceUtils.getIDFromDocumentSelfLink(state.documentSelfLink);
    Operation result = testEnvironment.sendPostAndWait(DhcpSubnetService.FACTORY_LINK, state);
    return result.getBody(DhcpSubnetService.State.class);
}

From source file:com.vmware.photon.controller.api.backend.workflows.RemoveFloatingIpFromVmWorkflowServiceTest.java

private static DhcpSubnetService.State createDhcpRootSubnetServiceInCloudStore(TestEnvironment testEnvironment)
        throws Throwable {
    String cidr = "192.168.1.0/24";
    SubnetUtils subnetUtils = new SubnetUtils(cidr);
    SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();

    DhcpSubnetService.State state = new DhcpSubnetService.State();
    state.cidr = cidr;/*from  w ww.  ja  v  a2  s  .  c  o  m*/
    state.lowIp = IpHelper.ipStringToLong(subnetInfo.getLowAddress());
    state.highIp = IpHelper.ipStringToLong(subnetInfo.getHighAddress());
    state.documentSelfLink = DhcpSubnetService.FLOATING_IP_SUBNET_SINGLETON_LINK;
    state.isFloatingIpSubnet = true;

    Operation result = testEnvironment.sendPostAndWait(DhcpSubnetService.FACTORY_LINK, state);
    return result.getBody(DhcpSubnetService.State.class);
}

From source file:com.eislab.af.translator.Translator_hub_i.java

private static String findOutgoingIpForGivenAdress(String remoteIP) {

    if (System.getProperty("os.name").contains("Windows")) {
        final String COMMAND = "route print -4";
        List<RouteInfo> routes = new ArrayList<>();
        try {/*  w  w  w .  ja  v a 2 s.  c o m*/
            Process exec = Runtime.getRuntime().exec(COMMAND);
            BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));

            System.out.println(System.getProperty("os.name"));
            String line;
            /* examples:
              0.0.0.0          0.0.0.0     10.172.180.1    10.172.180.36     20
              0.0.0.0          0.0.0.0      10.187.20.1    10.187.20.225     25
               10.172.180.0    255.255.255.0         On-link     10.172.180.36    276
              10.172.180.36  255.255.255.255         On-link     10.172.180.36    276
            */
            Pattern p = Pattern.compile(
                    "^\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+\\S+?\\s+(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+(\\d+)\\s*$");
            while ((line = reader.readLine()) != null) {
                Matcher match = p.matcher(line);
                if (match.matches()) {
                    String network = match.group(1);
                    String mask = match.group(2);
                    String address = match.group(3);
                    short maskLength = 0;
                    boolean networkMatch = network.contentEquals("0.0.0.0");

                    if (!networkMatch) {
                        SubnetUtils subnet = new SubnetUtils(network, mask);
                        SubnetUtils.SubnetInfo info = subnet.getInfo();
                        networkMatch = info.isInRange(remoteIP);
                        maskLength = Short.valueOf(info.getCidrSignature().split("/")[1]);
                    }

                    if (networkMatch) {
                        short metric = Short.valueOf(match.group(4));
                        routes.add(new RouteInfo(address, (short) 0, maskLength, metric));
                    }

                }
            }
            Collections.sort(routes);
            for (RouteInfo route : routes) {
            }

            if (!routes.isEmpty())
                return routes.get(0).source;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (System.getProperty("os.name").contains("Linux")) {

        List<RouteInfo> routes = new ArrayList<>();
        try {
            //ipv6 ^(.+)/(\d+)\s+(.+)\s(\d+)\s+(\d+)\s+(\d)\s+(.+)$
            //ipv4 ^\s+inet\s\addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+Bcast:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$
            //linux route get command parsing: ipv4 ^.*via.*\s+dev\s+.*\s+src\s((?:[0-9\.]{1,3})+)
            //linux route get comand parsing: ipv6 ^.*\sfrom\s::\svia.*\sdev\s.*\ssrc\s((?:[:]{1,2}|[0-9|a|b|c|d|e|f]{1,4})+)
            //final String COMMAND = "/sbin/ifconfig";
            final String COMMAND = "ip route get " + remoteIP;

            Process exec = Runtime.getRuntime().exec(COMMAND);
            BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));

            System.out.println(System.getProperty("os.name"));
            String line;
            /* examples:
             * 10.10.2.130 via 10.0.2.2 dev eth0  src 10.0.2.255
            */
            Pattern p = Pattern.compile("^.*via.*\\s+dev\\s+.*\\s+src\\s((?:[0-9|\\.]{1,3})+)");

            while ((line = reader.readLine()) != null) {
                Matcher match = p.matcher(line);
                if (match.matches()) {

                    String address = match.group(1);

                    routes.add(new RouteInfo(address, (short) 0, (short) 0, (short) 0));//metric is always 0, because we do not extract it from the ifconfig command.

                }
            }
            Collections.sort(routes);

            if (!routes.isEmpty())
                return routes.get(0).source;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return null;
}

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

public Text evaluate(Text inputaddr) throws UnknownHostException {

    String low_ip;/*  w  ww  .j  a v  a  2  s. c  o m*/
    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:com.unisa.hive.udf.getHighestIP.java

public Text evaluate(Text inputaddr) throws UnknownHostException {

    String high_ip;// w  w w .  j a v  a2s  .  c  om
    Text ip_out;

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

    ip_out = new Text(high_ip);

    return ip_out;
}