Subnet Calculator for IPV4 code example

Example: ip subnet calculator code

import java.util.*;
import java.util.regex.Pattern;
import java.math.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.net.*;


public class example {

    public static void main(String[] args) {
        Scanner Scan = new Scanner(System.in);
        System.out.print("Enter IP: ");
        String IP;
        IP = Scan.nextLine();
        String Z = IP.split("\\/")[1];
        String Y = IP.split("\\/")[0];
        String[] parts = Y.split("\\.");
        String F1 = parts[0];
        String F2 = parts[1];
        String F3 = parts[2];
        String F4 = parts[3];
        int A = Integer.parseInt(F1);
        int B = Integer.parseInt(F2);
        int C = Integer.parseInt(F3);
        int E = Integer.parseInt(F4);

        {
            System.out.println(F1 + "." + F2 + "." + F3 + "." + F4);
        }
        String X = (IP.substring(IP.lastIndexOf("/") + 1)); {
            System.out.println(X);
        }
        int S = Integer.parseInt(X);
        double Bits = 32;
        double TotalBites = Bits - S;
        double I = Math.pow(2, TotalBites);
        int D = (int)(I - 2); {
            System.out.println(D);
        }

        List < String > Hosts = new ArrayList < String > ();

        if (S >= 24) {
            int val = (int) TotalBites - 0;
            String bitsda = stringMultiply("0", val);
            String bitstoconvert = "1" + bitsda;
            int decimal = Integer.parseInt(bitstoconvert, 2) - 1;
            //String bits = Strings.repeat("0", val);

            for (int IPF4 = E; IPF4 <= decimal; IPF4++) {

                Hosts.add(F1 + "." + F2 + "." + F3 + "." + IPF4);
            }

        } else

        if (S >= 16) {

            int val = (int) TotalBites - 8;
            String bitsda = stringMultiply("0", val);
            String bitstoconvert = "1" + bitsda;
            int decimal = Integer.parseInt(bitstoconvert, 2) - 1;
            //String bits = Strings.repeat("0", val);
            for (int IPF3 = C; IPF3 <= decimal; IPF3++) {
                for (int IPF4 = E; IPF4 <= 255; IPF4++)
                    Hosts.add(F1 + "." + F2 + "." + IPF3 + "." + IPF4);

            }



        } else

        if (S >= 8) {

            int val = (int) TotalBites - 16;
            String bitsda = stringMultiply("0", val);
            String bitstoconvert = "1" + bitsda;
            int decimal = Integer.parseInt(bitstoconvert, 2) - 1;
            //String bits = Strings.repeat("0", val);

            for (int IPF2 = B; IPF2 <= decimal; IPF2++) {
                for (int IPF3 = C; IPF3 <= 255; IPF3++)
                    for (int IPF4 = E; IPF4 <= 255; IPF4++)
                        Hosts.add(F1 + "." + IPF2 + "." + IPF3 + "." + IPF4);


            }
        }

        Hosts.forEach(host -> pingIt(host));

    }

    public static void pingIt(String host) {
        try {
            String ipAddress = host;
            InetAddress inet = InetAddress.getByName(ipAddress);
            System.out.println("Sending Ping Request to " + ipAddress);
            if (inet.isReachable(5000)) {
                System.out.println(ipAddress + " is reachable.");
            } else {
                System.out.println(ipAddress + " NOT reachable.");
            }
        } catch (Exception e) {
            System.out.println("Exception:" + e.getMessage());
        }
    }


    public static String stringMultiply(String s, int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(s);
        }
        return sb.toString();
    }
}

Tags:

Misc Example