Last active
December 10, 2015 23:55
-
-
Save cupakromer/4512023 to your computer and use it in GitHub Desktop.
Find manufacturer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ap' | |
require 'csv' | |
class WifiManufacturer | |
include Enumerable | |
attr_reader :name, :oui_list | |
def initialize(name, ouis) | |
@name = name | |
@oui_list = ouis | |
end | |
def each(&block) | |
oui_list.each(&block) | |
end | |
def to_s | |
name.to_s | |
end | |
end | |
def unknown_manufacturer | |
@unknown ||= WifiManufacturer.new(:unknown, []) | |
end | |
def frequency | |
@frequency ||= Hash.new(0) | |
end | |
def tag_oui(oui, manufacturers) | |
manufacturer_bucket = manufacturers.find{ |manufacturer| manufacturer.include? oui } | |
manufacturer_bucket ||= unknown_manufacturer | |
frequency[manufacturer_bucket] += 1 | |
end | |
def load_ouis | |
# Replace with actual ouis | |
[ | |
WifiManufacturer.new(:apple, %w(00:11:22 11:22:33 22:33:44)), | |
WifiManufacturer.new(:samsung, %w(33:44:55 44:55:66 55:66:77)), | |
] | |
end | |
def csv_file | |
ENV['CSV_FILE'] || File.expand_path('~/temp.csv') | |
end | |
def unique_device_list | |
devices = {} | |
row_count = 0 | |
t1 = Time.now | |
CSV.foreach(csv_file, headers: :first_row) do |row| | |
row_count += 1 | |
mac, oui = row['mac'], row['oui'] | |
yield mac, oui if block_given? && devices[mac].nil? | |
devices[mac] ||= oui | |
end | |
t2 = Time.now | |
puts "Took #{t2 - t1} seconds" | |
puts "Total Data Rows: #{row_count}" | |
devices | |
end | |
oui_groups = load_ouis | |
total_devices = unique_device_list{ |device_hash, oui| | |
tag_oui(oui, oui_groups) | |
}.count | |
puts "Total Unique Devices: #{total_devices}" | |
ap frequency |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment