Skip to content

Instantly share code, notes, and snippets.

@cyberkov
Last active December 25, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cyberkov/7005859 to your computer and use it in GitHub Desktop.
Save cyberkov/7005859 to your computer and use it in GitHub Desktop.
Generate a range of MAC addresses
#!/usr/bin/env ruby
#require 'rubygems'
#require 'pry'
#require 'pp'
# This scripts returns a list of MAC addresses.
# Usage:
# ./$0 LIMIT START END
# ./genmac.rb 5 00:50:56:00:00:00 00:50:56:3F:FF:FF
#
# With limit you can set how many MAC addresses you want to have returned. Use 0 for unlimited results
# Start and end are obvious, end ist optional.
class String
def to_mac
MacAddress.new(self).to_s
end
end
class MacAddress
def initialize(str)
raise ArgumentError.new("Nil? Really?") if str.nil?
if str.is_a?(String)
@mac_str = str.strip
n = @mac_str.index(':')
if not n.nil? and n >= 12
@mac_str = @mac_str.split(':')[0]
end
@mac_str = @mac_str.downcase.gsub(/^0[xX]/,'').gsub(/[^0-9a-f]/,'')
raise ArgumentError.new("Invalid MAC address: #{str}") if @mac_str.length != 12
elsif str.is_a?(Fixnum) or str.is_a?(Integer)
@mac_str = str.to_s(16)
(12 - @mac_str.size).times {|x| @mac_str = '0' + @mac_str }
else
ArgumentError.new("Don't know what #{str} is.")
end
end
def to_i
@mac_str.hex
end
def to_s
arr = @mac_str.scan(/../)
arr.join(":")
end
def mac
@mac_str
end
end
begin
limit = ARGV[0].to_i unless ARGV[0].nil?
start = MacAddress.new(ARGV[1])
stop = MacAddress.new(ARGV[2])
rescue
end
start ||= MacAddress.new("00:50:56:00:00:00")
stop ||= MacAddress.new("FF:FF:FF:FF:FF:FF")
limit ||= -1
start.to_i.upto(stop.to_i).each do |x|
puts MacAddress.new(x).to_s
break if limit == 1
limit = limit - 1 unless limit == -1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment