-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_mk_country_acl.rb
executable file
·65 lines (57 loc) · 2.41 KB
/
gen_mk_country_acl.rb
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
#!/usr/bin/env ruby
#
# Generate Mikrotik RouterOS country ip blocks Address lists
#
# to do: flatten adjoining CIDR blocks where possible
#
require 'open-uri'
SOURCES = {
'arin' => 'ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest' ,
'afrinic' => 'ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest' ,
'apnic' => 'ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest' ,
'lacnic' => 'ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest' ,
'ripencc' => 'ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest'
}
CIDR = {
'16777216' => '/8', '8388608' => '/9', '4194304' => '/10',
'2097152' => '/11', '1048576' => '/12', '524288' => '/13',
'262144' => '/14', '131072' => '/15', '65536' => '/16',
'32768' => '/17', '16384' => '/18', '8192' => '/19',
'4096' => '/20', '2048' => '/21', '1024' => '/22',
'512' => '/23', '256' => '/24', '128' => '/25',
'64' => '/26', '32' => '/27', '16' => '/28',
'8' => '/29', '4' => '/30', '2' => '/31',
'1' => '/32'
}
def process_source(source)
source_uri = SOURCES[source]
# puts " ... processing source #{source_uri}"
File.open("mk_#{source}.rsc",'w') {|fo|
fo.puts "###############################################"
fo.puts "# Mikrotik RouterOS script file auto-generated"
fo.puts "# script: github.com/audric/gen_mk_country_acl"
fo.puts "# ip source: #{source_uri}"
fo.puts "###############################################"
fo.puts "/ip firewall address-list"
open(source_uri) {|fi|
fi.each_line {|line|
if !line.start_with?("#", "*", " ") then
(source, country, rectype, ip, num, update, dummy ) = line.split("|")
if rectype == 'ipv4' and country != '*' then
cidr = CIDR[num]
fo.puts "add address=#{ip}#{cidr} list=\"#{country} country\" comment=\"source #{source} #{update}\""
end
end
}
}
}
end
def process_all
SOURCES.each do |source_name,source_uri|
# puts " ... about to process source #{source_name} with uri #{source_uri}"
process_source(source_name)
end
end
#process_source('ripencc')
process_all
exit