-
Notifications
You must be signed in to change notification settings - Fork 0
/
ios4r2.rb
149 lines (121 loc) · 3.19 KB
/
ios4r2.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
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
require 'strscan'
require 'yaml'
require 'pp'
require 'rubygems'
require 'treetop'
require 'storage'
class Parser
attr_accessor :config, :token, :tokens_list, :parsed_hashes, :token_instruction, :token_instruction_subconf, :index, :grammar
@parser
def initialize
@config = IO.readlines(ARGV[0])
@tokens_list = Array.new
@parsed_hashes = Hash.new
@token_instruction_subconf = Hash.new
end
def find_lines
token_regexp = Regexp.new(@token)
@config.each_index {|index|
scan = StringScanner.new(@config[index])
if scan.scan(token_regexp) == @token
@tokens_list << @config[index]
end
}
end
def find_token_instruction_subconf
token_instruction_regexp = Regexp.new(@token_instruction)
index = 1
until index == @config.length do
scan = StringScanner.new(@config[index])
if scan.scan(token_instruction_regexp) == @token_instruction
pointer = index
until index == @config.length do
pointer +=1
token_regexp = Regexp.new(@token)
scan2 = StringScanner.new(@config[pointer])
until scan2.scan(token_regexp) == @token or @config[pointer].include? "!" do
@token_instruction_subconf.update({pointer+1 => @config[pointer]})
pointer += 1
scan2 = StringScanner.new(@config[pointer])
end
break
end
index = pointer
else
index += 1
end
end
end
def load_grammar
dir = File.dirname(__FILE__)
Treetop.load dir + "/" + grammar
parsername = grammar.capitalize + "Parser"
@parser = Object.module_eval("::#{parsername}").new
end
def use_grammar_on(an_array)
an_array.each_pair {|key, value|
result = @parser.parse(value)
values = Hash.new
result.singleton_methods.each {|method|
begin
value = {method => result.send(method).text_value}
values.update(value)
end
}
@parsed_hashes.update({key => values})
}
end
def find_grammar
if @token.include? " " or @token.include? "-"
# limitations de TreeTop
@grammar = @token.gsub(/ /,'_').gsub(/-/,'_')
else
@grammar = @token
end
end
def grammar_fail
@parsed_hashes.each_pair{|key, value|
if value.to_s == ""
p "can't parse '#{@token_instruction_subconf[key]}' at line number '#{key}'"
end
}
end
end
s = Storage.new('ios.tct')
z = Parser.new
z.token = "ip access-list extended"
z.find_lines
z.tokens_list.each{|token|
z.token_instruction = token
#z.token_instruction = z.tokens_list[7]
#p z.token_instruction
z.find_token_instruction_subconf
#pp z.token_instruction_subconf
#z.find_grammar
#z.load_grammar
#z.use_grammar_on(z.tokens_list[2])
z.grammar = "ios_acl"
z.load_grammar
#z.token_instruction = z.tokens_list[4]
#p z.token_instruction_subconf
z.use_grammar_on(z.token_instruction_subconf)
z.grammar_fail
#pp z.parsed_hashes
@temp1 = Hash.new
z.parsed_hashes.each_pair{|key, value|
@temp1.update({"index" => key})
value.each_pair{|key2, value2|
@temp1.update({key2 => value2})
}
@temp1.update({'parent' => z.token_instruction})
@temp1.update({'type' => 'acl'})
@temp1.update({'time' => Time.now.to_i})
@temp1.update({'file' => ARGV[0]})
s.add(@temp1)
}
}
pp s.db.query { |q|
q.add_condition 'type', :equals, 'acl'
q.order_by 'destination_ip'
}
s.db.close