@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Ruby: CIDR 表記を IP アドレスのリストに展開する

IPAddr を使う。

require 'ipaddr'
ip = IPAddr.new('192.168.0.1/24')
puts ip.to_range.to_a.first(5)
puts ip.to_range.to_a.last(5)
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.251
192.168.0.252
192.168.0.253
192.168.0.254
192.168.0.255