Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #require 'rubygems'
  3. #require 'pry'
  4. #require 'pp'
  5.  
  6. # This scripts returns a list of MAC addresses.
  7. # Usage:
  8. # ./$0 LIMIT START END
  9. # ./genmac.rb 5 00:50:56:00:00:00 00:50:56:3F:FF:FF
  10. #
  11. # With limit you can set how many MAC addresses you want to have returned. Use 0 for unlimited results
  12. # Start and end are obvious, end ist optional.
  13.  
  14. class String
  15. def to_mac
  16. MacAddress.new(self).to_s
  17. end
  18. end
  19.  
  20. class MacAddress
  21. def initialize(str)
  22. raise ArgumentError.new("Nil? Really?") if str.nil?
  23. if str.is_a?(String)
  24. @mac_str = str.strip
  25. n = @mac_str.index(':')
  26. if not n.nil? and n >= 12
  27. @mac_str = @mac_str.split(':')[0]
  28. end
  29. @mac_str = @mac_str.downcase.gsub(/^0[xX]/,'').gsub(/[^0-9a-f]/,'')
  30. raise ArgumentError.new("Invalid MAC address: #{str}") if @mac_str.length != 12
  31. elsif str.is_a?(Fixnum) or str.is_a?(Integer)
  32. @mac_str = str.to_s(16)
  33. (12 - @mac_str.size).times {|x| @mac_str = '0' + @mac_str }
  34. else
  35. ArgumentError.new("Don't know what #{str} is.")
  36. end
  37. end
  38.  
  39. def to_i
  40. @mac_str.hex
  41. end
  42.  
  43. def to_s
  44. arr = @mac_str.scan(/../)
  45. arr.join(":")
  46. end
  47.  
  48. def mac
  49. @mac_str
  50. end
  51. end
  52.  
  53. begin
  54. limit = ARGV[0].to_i unless ARGV[0].nil?
  55. start = MacAddress.new(ARGV[1])
  56. stop = MacAddress.new(ARGV[2])
  57. rescue
  58. end
  59.  
  60. start ||= MacAddress.new("00:50:56:00:00:00")
  61. stop ||= MacAddress.new("FF:FF:FF:FF:FF:FF")
  62. limit ||= -1
  63.  
  64. start.to_i.upto(stop.to_i).each do |x|
  65. puts MacAddress.new(x).to_s
  66.  
  67. break if limit == 1
  68. limit = limit - 1 unless limit == -1
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement