Guest User

Untitled

a guest
Jan 12th, 2013
392
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ["192.168.2.0",
  2. "192.168.2.1",
  3. "192.168.2.2",
  4. "192.168.2.3",
  5. "192.168.2.4",
  6. "192.168.2.5"]
  7.  
  8. ip = '192.168.2.0'
  9.  
  10. ips = 0.upto(5).map do |n|
  11. ip.sub(/.d+$/, '.' << n.to_s)
  12. end
  13.  
  14. require 'ipaddr'
  15. ip = "192.168.2.0"
  16.  
  17. ips = [ip]
  18. 5.times do
  19. ips << IPAddr.new(ips.last).succ.to_s
  20. end
  21. p ips
  22. # =>["192.168.2.0", "192.168.2.1", "192.168.2.2", "192.168.2.3", "192.168.2.4", "192.168.2.5"]
  23.  
  24. #Ranges work:
  25. ip_range = IPAddr.new("192.168.2.0")..IPAddr.new("192.168.2.5")
  26. p ip_range.map(&:to_s)
  27. # =>["192.168.2.0", "192.168.2.1", "192.168.2.2", "192.168.2.3", "192.168.2.4", "192.168.2.5"]
  28.  
  29. ip = '192.168.2.0'
  30.  
  31. p (0...5).inject([ip]) { |l| l << l.last.succ }
  32.  
  33. #=> ["192.168.2.0", "192.168.2.1", "192.168.2.2", "192.168.2.3", "192.168.2.4", "192.168.2.5"]
  34.  
  35. p (0..255).inject([ip]) { |l, d| l << (l.last.split('.')[0..2] << d).join('.') }
  36.  
  37. #=> ["192.168.2.0" ... "192.168.2.255"]
  38.  
  39. require 'ipaddr'
  40. current = ip = IPAddr.new('192.168.2.0')
  41. array = [ip]
  42. until current.to_s == '192.168.2.5'
  43. array << current = current.succ
  44. end
  45. array.map!(&:to_s)
  46.  
  47. ip = '192.168.2.0'
  48.  
  49. ips = 0.upto(5).map do |n|
  50. ip.split('.').tap{|i| i[-1] = n }.join('.')
  51. end
RAW Paste Data