iosparse 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/iosparse.gemspec +1 -1
- data/lib/iosparse.rb +72 -22
- metadata +1 -1
data/README.md
CHANGED
@@ -2,7 +2,7 @@ IOSParse will parse Cisco IOS configuration files, more functionality coming.
|
|
2
2
|
|
3
3
|
# How do I use it?
|
4
4
|
Good question. First install it using `gem install iosparse`.
|
5
|
-
Then use it. Create a new IOSParse object
|
5
|
+
Then use it. Create a new IOSParse object passing it the filename of the Cisco IOS config file (usually a show all type of dump file). Then ask it questions:
|
6
6
|
|
7
7
|
````
|
8
8
|
config = IOSParse.new('path/to/ios.conf')
|
data/iosparse.gemspec
CHANGED
data/lib/iosparse.rb
CHANGED
@@ -16,23 +16,33 @@ class IOSParse
|
|
16
16
|
end
|
17
17
|
|
18
18
|
#
|
19
|
-
# Find all interfaces
|
19
|
+
# Find all interfaces
|
20
20
|
#
|
21
21
|
def interfaces
|
22
22
|
# Regex to parse out interfaces
|
23
23
|
interfaces = /\["interface[\s\S]+/
|
24
|
-
# Call
|
25
|
-
|
24
|
+
# Call find_blocks() passing regex, parent name, and parse boolean
|
25
|
+
find_blocks(interfaces, "interface", false)
|
26
26
|
end
|
27
27
|
|
28
28
|
#
|
29
|
-
# Find all
|
29
|
+
# Find all interfaces in monitor mode
|
30
|
+
#
|
31
|
+
def monitor_interfaces
|
32
|
+
# Regex to parse out the monitor interfaces
|
33
|
+
interfaces = /monitor-interface.+/
|
34
|
+
# Call find_lines() passing regex, parent name, and parse boolean
|
35
|
+
find_lines(interfaces, "monitor-interface", false)
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Find all names
|
30
40
|
#
|
31
41
|
def names
|
32
42
|
# Regex to parse out names
|
33
43
|
names = /\["names[\s\S]+/
|
34
|
-
# Call
|
35
|
-
|
44
|
+
# Call find_blocks() passing regex, parent name, and parse boolean
|
45
|
+
find_blocks(names, "names", false)
|
36
46
|
end
|
37
47
|
|
38
48
|
#
|
@@ -41,18 +51,18 @@ class IOSParse
|
|
41
51
|
def routes
|
42
52
|
# Regex to parse out routes
|
43
53
|
routes = /\["route[\s\S]+/
|
44
|
-
# Call
|
45
|
-
|
54
|
+
# Call find_blocks() passing regex, parent name, and parse boolean
|
55
|
+
find_blocks(routes, "route", false)
|
46
56
|
end
|
47
57
|
|
48
58
|
#
|
49
|
-
# Find all
|
59
|
+
# Find all access-lists
|
50
60
|
#
|
51
61
|
def access_list
|
52
62
|
# Regex to parse out acl
|
53
63
|
acl = /access-list[\s\S]+/
|
54
|
-
# Call
|
55
|
-
|
64
|
+
# Call find_blocks() passing regex, parent name, and parse boolean
|
65
|
+
find_blocks(acl, "access-list", true)
|
56
66
|
end
|
57
67
|
|
58
68
|
#
|
@@ -61,8 +71,8 @@ class IOSParse
|
|
61
71
|
def object_group
|
62
72
|
# Regex to parse out object-groups
|
63
73
|
group = /object-group[\s\S]+/
|
64
|
-
# Call
|
65
|
-
|
74
|
+
# Call find_blocks() passing regex, parent name, and parse boolean
|
75
|
+
find_blocks(group, "object-group", true)
|
66
76
|
end
|
67
77
|
|
68
78
|
private
|
@@ -70,7 +80,7 @@ class IOSParse
|
|
70
80
|
#
|
71
81
|
# Primary method to accept regex and parent, then use them to parse blocks and store them in an Array
|
72
82
|
#
|
73
|
-
def
|
83
|
+
def find_blocks(filter, parent, parse)
|
74
84
|
# Array to store parsed blocks
|
75
85
|
output = Array.new
|
76
86
|
@blocks.each do |block|
|
@@ -79,7 +89,7 @@ class IOSParse
|
|
79
89
|
end
|
80
90
|
# Some parents are not within "!" blocks and require some extra work
|
81
91
|
if parse == true then
|
82
|
-
parse_parent(
|
92
|
+
parse_parent(clean_parent(output), parent)
|
83
93
|
else
|
84
94
|
# Return the parsed block as an array
|
85
95
|
output
|
@@ -87,9 +97,28 @@ class IOSParse
|
|
87
97
|
end
|
88
98
|
|
89
99
|
#
|
90
|
-
#
|
100
|
+
# Primary method to accept regex and parent, then use them to parse lines and store them in an Array
|
91
101
|
#
|
92
|
-
def
|
102
|
+
def find_lines(filter, parent, parse)
|
103
|
+
# Array to store parsed blocks
|
104
|
+
output = Array.new
|
105
|
+
@blocks.each do |block|
|
106
|
+
# Use regex to filter via scan() then split each line
|
107
|
+
block.to_s.match(filter).to_s.split('\n').each do |line|
|
108
|
+
# Skip unless the parent is in the line
|
109
|
+
next unless line.match(filter)
|
110
|
+
# Push matches to array
|
111
|
+
output << normalize_line(line.match(filter))
|
112
|
+
end
|
113
|
+
end
|
114
|
+
# Return the parsed block as an array
|
115
|
+
output
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# Remove extra characters from parents
|
120
|
+
#
|
121
|
+
def clean_parent(block)
|
93
122
|
# Array to store parsed blocks
|
94
123
|
output = Array.new
|
95
124
|
block.each do |line|
|
@@ -101,23 +130,44 @@ class IOSParse
|
|
101
130
|
end
|
102
131
|
|
103
132
|
#
|
104
|
-
# Some parents come as a single string, we break each
|
133
|
+
# Some parents come as a single string, we break each parent into a string and push into an array
|
105
134
|
#
|
106
135
|
def parse_parent(block, parent)
|
107
136
|
# Array to store parsed blocks
|
108
137
|
output = Array.new
|
109
138
|
block.each do |line|
|
110
139
|
# Split the line where each parent begins
|
111
|
-
line.to_s.split(parent).each do |
|
140
|
+
line.to_s.split(parent).each do |data|
|
112
141
|
# Skip empty lines
|
113
|
-
next if
|
142
|
+
next if data.include?('["')
|
114
143
|
# Remove extra "] characters from each line
|
115
|
-
|
144
|
+
data = data.gsub('"]', '')
|
116
145
|
# Re-add parent to the beginning of the line and push it into the array line-by-line
|
117
|
-
output << "[\"#{parent}#{
|
146
|
+
output << "[\"#{parent}#{data}\"]"
|
118
147
|
end
|
119
148
|
end
|
120
149
|
# Return the parsed block as an array
|
121
150
|
output
|
122
151
|
end
|
152
|
+
|
153
|
+
#
|
154
|
+
# Normalize block to standardize returned arrays
|
155
|
+
#
|
156
|
+
def normalize_block(block)
|
157
|
+
# Array to store normalized data
|
158
|
+
output = Array.new
|
159
|
+
block.each do |data|
|
160
|
+
# Normalize data
|
161
|
+
output << "[\"#{data}\\\\n\"]"
|
162
|
+
end
|
163
|
+
# Return the normalized array
|
164
|
+
output
|
165
|
+
end
|
166
|
+
|
167
|
+
#
|
168
|
+
# Normalize line to standardize returned arrays
|
169
|
+
def normalize_line(data)
|
170
|
+
# Return normalized data
|
171
|
+
"[\"#{data}\\\\n\"]"
|
172
|
+
end
|
123
173
|
end
|