Generate random, valid IP address

435 views
Skip to first unread message

Radhesh

unread,
Mar 17, 2011, 5:10:05 PM3/17/11
to Ruby IPAddress
Hi,

I am trying to test some firewall code I have written in ruby, and I
found that IPAddress would be great for IP address generation.
There does not appear to be a random address generator in IPAddress,
though.

How could I write my own? Also, it might be a great feature to add to
IPAddress, as I am sure many people would like to generate random IP
addresses.

Best,
Radhesh

Marco Ceresa

unread,
Mar 29, 2011, 2:35:30 AM3/29/11
to ruby-ip...@googlegroups.com, Radhesh
On Thu, Mar 17, 2011 at 10:10 PM, Radhesh <rmk....@gmail.com> wrote:
> Hi,
>
> I am trying to test some firewall code I have written in ruby, and I
> found that IPAddress would be great for IP address generation.
> There does not appear to be a random address generator in IPAddress,
> though.

Hello Radhesh,

interesting idea! Let's see what people in the list think, maybe if we
have enough demand we can definitely include a method for random IP's.

> How could I write my own? Also, it might be a great feature to add to
> IPAddress, as I am sure many people would like to generate random IP
> addresses.

I would maybe go in this way:

ip = (rand(255)<<24)+(rand(255)<<16)+(rand(255)<<8)+rand(255)
IPAddress::IPv4::parse_u32 ip

Reply all
Reply to author
Forward
0 new messages