From: Peter Ertl Date: 2006-04-21T02:08:11+09:00 Subject: Re: Converting IP range to array of IP's the code it's broken / working fine depending on how you define a range... lets say you have a 192.168.0.0/10 subnet: 192.168.0.1 - 192.168.3.254 will do a 'wraparound' and emit addresses like 192.168.1.0 (which is a subnet but no host) 192.168.1.255 (which is the broadcast) but these are not legal ip addresses. this is inconvenient if you want to address all hosts in a subnet I would define range in our case: "all ips between ip1 and ip2 where _each_ of the digits is between the corresponding digit in ip1 and ip2" example: 192.168.1.3 - 192.168.2.4: [192, 168, 1, 3] [192, 168, 1, 4] [192, 168, 2, 3] [192, 168, 2, 4] > --- Urspr�ngliche Nachricht --- > Von: "Paul Battley" > An: ruby-talk@ruby-lang.org (ruby-talk ML) > Betreff: Re: Converting IP range to array of IP's > Datum: Fri, 21 Apr 2006 01:52:27 +0900 > > On 20/04/06, Kris wrote: > > I'm looking to convert an IP string (ranges, subnet masks or wild-carded > > IP's) in to an array of IP strings: > > The dotted notation is just a human-readable version of a 32-bit > value, so you can do this: > > def dq_to_int(dq) > parts = dq.split('.').map{ |s| s.to_i }.reverse > (0..3).inject(0){ |r,i| r + parts[i] * (0x100**i) } > end > > def int_to_dq(int) > (0..3).map{ |i| ((int / (0x100**(3-i))) & 0xFF).to_s }.join('.') > end > > (dq_to_int('192.168.1.1')..dq_to_int('192.168.1.5')).map{ |i| int_to_dq(i) > } > # => ["192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4", > "192.168.1.5"] > > Paul.