Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

LAN Range - Java Code Bank


LAN Range
Find reachable address within a given range.
                    // IPAnalyst.java
     
    /**
     * @author - yllypsys
     */
     
    package ipanalyst;
     
    public class IPAnalyst {
        public static void main(String[] args) {
            Ranger ranger = new Ranger();
           
            // Check the IsUP method for more notes.
            // 2012 is the best timeout I have found.

            // You could take the range as a parameter
            // but you would need to check the format before scanning.
            ranger.scan("0.0.0.0", "255.255.255.255", 2012);
        }
    }
     
    // EOF
     
    // Ranger.java
     
    /**
     * @author - yllypsys
     */
     
    package ipanalyst;
     
    import java.io.IOException;
    import java.net.InetAddress;
     
    public class Ranger
    {
        public void scan(String start, String stop, int timeout)
        {
            // Arrays used for storing each IP segment.
            String[] startArray = new String[4]; String[] stopArray = new String[4];
            // Size meters used to bounds check the given addresses.
            // These will never fail since we hardcoded the addresses.
            // See IPAnalyst::main
            int startArrayLength = 0; int stopArrayLength = 0;
     
            // Arrays used for storing numeric representations of each IP segment.
            int[] sta = new int[4]; int[] sto = new int[4];
     
            // Initialize the string arrays.
            for (int i = 0; i < 4; i++)
            {
                startArray[i] = "";
                stopArray[i] = "";
            }
     
            // For each char in start address...
            for (int i = 0; i < start.length(); i++)
            {
                // Test the current character...
                switch (start.charAt(i))
                {
                    // If current char is '.'
                    case '.':
                        // Increase the size of the size meter for start array.
                        startArrayLength++;
                    break;
                    // If current char is not '.'
                    default:
                        // Current size meter length of start array.
                        int length = startArrayLength;
                        // Add the char to the start array at appropriate index.
                        startArray[length] = (startArray[length] + start.charAt(i));
                    break;
                }
            }
     
            for (int i = 0; i < stop.length(); i++)
            {
                switch (stop.charAt(i))
                {
                    case '.':
                        stopArrayLength++;
                    break;
                       
                    default:
                        int length = stopArrayLength;
                        stopArray[length] = (stopArray[length] + stop.charAt(i));
                    break;
                }
            }
     
            // For each item in start array.
            for (int i = 0; i <= startArrayLength; i++)
            {
                // Convert string value to integer value.
                // Store in array of numeric representation of IP segments.
                sta[i] = Integer.valueOf(startArray[i].toString()).intValue();
                sto[i] = Integer.valueOf(stopArray[i].toString()).intValue();
            }
     
            // Marker to know when not to format stdout.
            int first = 1;
     
            // If the start address is lower than the stop address...
            if ((sta[0] <= sto[0]) &&
                (sta[1] <= sto[1]) &&
                (sta[2] <= sto[2]) &&
                (sta[3] <= sto[3]))
            {
                // Loop through each address segment...
                for (int a = sta[0]; a <= sto[0]; a++)
                {
                    for (int b = sta[1]; b <= sto[1]; b++)
                    {
                        for (int c = sta[2]; c <= sto[2]; c++)
                        {
                            for (int d = sta[3]; d <= sto[3]; d++)
                            {
                                // Temporary string containing the current address.
                                String address = Integer.toString(a) + "." + Integer.toString(b) + "." + Integer.toString(c) + "." + Integer.toString(d);
                               
                                // If the IP is reachable...
                                if (isUP(a, b, c, d, timeout))
                                {
                                    // If this is the first reachable address...
                                    if (first == 1)
                                        // Print without formating...
                                        System.out.print(address);
                                    else
                                        System.out.print("\r\n" + address);
                                   
                                    // The first reachable address has been found.
                                    first++;
                                }
                            }
                        }
                    }
                }
            }
        }
     
        // a, b, c, d = IP address segments...
        // timeout = mutable value which you can change to your liking.
        public boolean isUP(int a, int b, int c, int d, int timeout)
        {
            try
            {
                // Byte array of the IP address.
                byte[] address = { (byte)a, (byte)b, (byte)c, (byte)d };
                // Object containing address details.
                InetAddress addr = InetAddress.getByAddress(address);
               
                // You could use isReachable(NetworkInterface, TTL, Timeout)
                // You could enumerate the interfaces and scan against each
                // but I will leave that up to you.
                // For the sake of simplicity, I have only supplied a timeout.
     
                // If it is reachable, it will return true.
                // Otherwise, it returns false.
                return addr.isReachable(timeout);
            }
            catch (IOException e)
            {
            }
           
            // Default return value. In case an exception is raised.
            return (false);
        }
    }
     
    // EOF

            
Comments
Arabian's avatar
Arabian 12 years ago

cool O(n^4) search complexity, brah. Try threading it next time.

ellipsis's avatar
ellipsis 12 years ago

Thanks, dewd.