Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ["192.168.2.0",
- "192.168.2.1",
- "192.168.2.2",
- "192.168.2.3",
- "192.168.2.4",
- "192.168.2.5"]
- ip = '192.168.2.0'
- ips = 0.upto(5).map do |n|
- ip.sub(/.d+$/, '.' << n.to_s)
- end
- require 'ipaddr'
- ip = "192.168.2.0"
- ips = [ip]
- 5.times do
- ips << IPAddr.new(ips.last).succ.to_s
- end
- p ips
- # =>["192.168.2.0", "192.168.2.1", "192.168.2.2", "192.168.2.3", "192.168.2.4", "192.168.2.5"]
- #Ranges work:
- ip_range = IPAddr.new("192.168.2.0")..IPAddr.new("192.168.2.5")
- p ip_range.map(&:to_s)
- # =>["192.168.2.0", "192.168.2.1", "192.168.2.2", "192.168.2.3", "192.168.2.4", "192.168.2.5"]
- ip = '192.168.2.0'
- p (0...5).inject([ip]) { |l| l << l.last.succ }
- #=> ["192.168.2.0", "192.168.2.1", "192.168.2.2", "192.168.2.3", "192.168.2.4", "192.168.2.5"]
- p (0..255).inject([ip]) { |l, d| l << (l.last.split('.')[0..2] << d).join('.') }
- #=> ["192.168.2.0" ... "192.168.2.255"]
- require 'ipaddr'
- current = ip = IPAddr.new('192.168.2.0')
- array = [ip]
- until current.to_s == '192.168.2.5'
- array << current = current.succ
- end
- array.map!(&:to_s)
- ip = '192.168.2.0'
- ips = 0.upto(5).map do |n|
- ip.split('.').tap{|i| i[-1] = n }.join('.')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement