我试图从给定范围获取随机IP地址。
EX:startIp =“192.168.1.0”; endIp =“192.168.2.255”
我尝试将范围转换为cidr并使用SubnetUtils获取cidr列表的randomIp,但没有运气。
是否有任何有效的方法从给定的ip-range或api生成随机ip可以做到这一点?
提前致谢。
您可以按照以下步骤实现:
InetAddress i= InetAddress.getByName(IPString);
int intRepresentation= ByteBuffer.wrap(i.getAddress()).getInt();
r.nextInt(High-Low) + Low;
i= InetAddress.getByName(String.valueOf(intRepresentation));
String ip= i.getHostAddress();