My program gets as input parameter a String containing a list of IP Addresses. Each IP address is separated by a line break. It can look like this:
10.1.1.1 2.2.2.2 11.1.1.1
it can look like this
10.1.1.1-20 1.1.1.1
but it can looki like this
172.16.12.1-20 /24 10.1.1.1
I want to check every IP address and return two Lists validAddresses, invalidAddresses.
I've already wrote a program that deals with the first the simplest type of input, i.e. no IP address ranges and no network masks.
private String[] extractIPAddress(String address){
String[] temp;
temp = address.split("\\s+");
return temp;
}
Then I do
addressList = extractIPAddress(String.valueOf(value));
for (int i=0; i < addressList.length; i++) {
if (InetAddresses.isInetAddress(addressList[i]) == true) {
validAddress = validAddress.concat(addressList[i] + '\n');
} else {
invalidAddress = invalidAddress.concat(addressList[i] + '\n');
}
}
Now I'm pondering how to deal with the most complex type of input, esp.
when the line has a range attached to it 1.1.1.1-10, how to remove the -10 part in order check the main IP address; how to check whether range part -10 would make a valid IP address i.e. 1.1.1.10 and then how to put everything together, so I can return it as a line of the validAddress String, looking the same way as at the beginning, i.e. 1.1.1.1-10
same question applies to the network mask /24
What elements would this kind of program have? Could you outline it for me?
I thought I would do the following, but I'm not sure if that's the right way and how to implement some parts:
- then cut off the part starting at the position of - until end of line or "/" (how to do that?)ipRange variable/ then cut off that part starting at the position of / until the end of the linenetMask variable tmp_ipRange = ipRange- in the tem_ipRange variabletmp_ipRange (how to do that?)String.split() (impossible, because you can't just add something to an array in java? what alternative do I have? so I can't use split here?)addressList (see above code) and check if the IP address is a valid IP addressipRange to the main ipAddress if ipRange is not null (how do I find the main ipAddress the ipRange belongs to?)netMaske to the main ipAddress (and range) if mainAddress is not null (how do I find the main ipAddress the netMask belongs to?)The parsing part could be done by a regular expression. Something like:
final Pattern p = Pattern.compile(
"(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:-(\\d+))?(?:/(\\d+))?" );
for(String line : new String [] { "172.16.12.1-20/24",
"172.16.12.1-20",
"172.16.12.1/24",
"172.16.12.1" })
{
Matcher m = p.matcher(line);
if (m.matches()) {
String address = m.group(1);
String rangePart = m.group(2); // is null if there is no range part
String netmask = m.group(3); // is null if there is no netmask
System.out.println(address + " - " + rangePart + " - " + netmask);
}
}
Edit: If you need to deal with spaces, you can augment the regular expression by adding \\s*, for example:
"\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:\\s*-\\s*(\\d+))?(?:\\s*/\\s*(\\d+))?\\s*"
This way, you won't need to bother with spaces.
You could also create a similar regular expression for IPv6 addresses. It will be longer, of course, but the principle is the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With