-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathupdate
executable file
·150 lines (123 loc) · 3.33 KB
/
update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env ruby
require "timeout"
require "net/http"
require "uri"
require "csv"
#
# MAC Age Tracker
#
@macs = {}
@today = Time.now.strftime("%Y-%m-%d")
@based = File.expand_path(File.dirname(__FILE__))
Dir.chdir(@based)
def update(addr, date, source)
if ! @macs[addr]
@macs[addr] = [date, source]
return
end
odate = @macs[addr].first.gsub("-", "").to_i
ndate = date.gsub("-", "").to_i
# Overwrite if new record is older
if ndate < odate
@macs[addr] = [date, source]
end
end
def load_current
CSV.new(File.read(File.join(@based, "data", "mac-ages.csv")),:liberal_parsing => true).each do |row|
if row[0].index("/").nil?
row[0] << "/24"
end
addr_base,addr_mask = row[0].downcase.split("/")
addr_base = mac_pad(addr_base)
date = row[1]
src = row[2]
update(addr_base + "/" + addr_mask, date, src)
end
end
def mac_pad(addr)
addr.ljust(12, "0")
end
def load_ieee_urls
ieee_urls = [
["https://standards-oui.ieee.org/oui/oui.csv", 29227],
["https://standards-oui.ieee.org/cid/cid.csv", 115],
["https://standards-oui.ieee.org/iab/iab.csv", 4576],
["https://standards-oui.ieee.org/oui28/mam.csv", 3484],
["https://standards-oui.ieee.org/oui36/oui36.csv", 4154],
]
ieee_urls.each do |url_info|
url_path, url_min_records = url_info
Timeout.timeout(300) do
records = download_ieee(url_path)
if records.nil?
raise RuntimeError, "URL #{url_path} returned zero records (wanted >= #{url_min_records})"
end
if records.length < url_min_records
raise RuntimeError, "URL #{url_path} only has #{records.length} records (wanted >= #{url_min_records})"
end
records.each do |info|
next if info[0] =~ /^Registry,/
addr_base = info[1]
if addr_base.nil?
$stderr.puts "URL #{url_path} has bad line: #{rec}"
next
end
addr_mask = ((addr_base.length / 2.0) * 8).to_i
addr_base = mac_pad(addr_base)
next if addr_base !~ /^[a-f0-9]+$/i
addr = "#{addr_base}/#{addr_mask}".downcase
update(addr, @today, "ieee-#{File.basename(url_path)}")
end
end
end
end
def download_ieee(url)
name = File.basename(url)
path = File.join(@based, "data", "ieee", name)
retries = 0
begin
data = Net::HTTP.get(URI(url))
rescue ::Timeout, ::Interrupt
raise $!
rescue ::Exception
if retries > 5
raise $1
end
retries += 1
sleep(5)
retry
end
return CSV.parse(data, :liberal_parsing => true)
end
def sortable_prefix(str)
prefix, mask = str.split("/")
mask = mask.rjust(2, "0")
mask + prefix
end
def write_results
fd = File.open(File.join(@based, "data", "mac-ages.csv"), "wb")
@macs.keys.sort{|a,b| sortable_prefix(b) <=> sortable_prefix(a) }.
each do |mac|
fd.puts [mac, @macs[mac][0], @macs[mac][1]].join(",")
end
fd.close
end
def log(msg)
$stdout.puts "#{Time.now.to_s} #{msg}"
$stdout.flush
end
log("Starting update for #{@today.to_s}")
# Sources
log("Loading current dataset")
load_current()
log("Loading the IEEE URLs")
old_count = @macs.keys.length
load_ieee_urls()
new_count = @macs.keys.length
# Generate merged output
log("Writing results for #{@macs.keys.length} entries (#{old_count} -> #{new_count})")
write_results()
log("Commiting and pushing the results")
# Commit the results
system("git commit -m 'Update #{@today}' -a && git push origin main")
exit(0)