java - Get all IP addresses from a given IP address and subnet mask -
In Java, I need to get a list of all IP addresses contained by an IP network.
For example, having networking: 192.168.5.0/24 then the output will be (192.168.0.0 ... 192.168.5.255).
I could think of the following method but it looks dirty, is any great way? There is no function in InetAddress
category.
-
Obtain Network IP from Input IP and Subnet Mask
Mask = (tall) (0xffffffff)
The function 'getLongfromIp' contains the code -
-
hosted by subnet mask
MaxRange = (long) 0x1 < & Lt; (32-subnet mask);
Get the address of Convert the IP from above step to octet string.
Ps: I'm sure IP addresses will be in IPv4 only.
To answer your question, resolve Apache commons.net library
Import org.apache.commons.net.util is to use. ; SubnetUtils Utility = New Subnetuits ("192.168.1.0/24"); String [] allIps = utils.getInfo (). GetAllAddresses (); // appIps will have all IP addresses in the subnet
Read more:
-
Comments
Post a Comment