iosparse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile +3 -0
  2. data/README.md +19 -0
  3. data/iosparse.gemspec +18 -0
  4. data/lib/iosparse.rb +113 -0
  5. metadata +48 -0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ IOSParse will parse Cisco IOS configuration files, more functionality coming.
2
+
3
+ # How do I use it?
4
+ Good question. Create a new IOSParse object and pass it the full path including filename of the Cisco IOS config file (usually a show all type of dump file). Then ask it questions:
5
+
6
+ ````
7
+ config = IOSParse.new('ios.conf')
8
+ puts config.interfaces
9
+ ````
10
+ ````
11
+ Output:
12
+
13
+ [0] => ["interface Ethernet0/0\n ip address 1.1.2.1 255.255.255.0\n no cdp enable\n"]
14
+ [1] => ["interface Serial1/0\n encapsulation ppp\n ip address 1.1.1.1 255.255.255.252\n"]
15
+ [2] => ["interface Serial1/1\n encapsulation ppp\n ip address 1.1.1.5 255.255.255.252\n service-policy output QOS_1\n"]
16
+ [3] => ["interface Serial1/2\n encapsulation hdlc\n ip address 1.1.1.9 255.255.255.252\n"]
17
+ ````
18
+
19
+ All objects returned are arrays that can be further manipulated and parsed. "Human readable" format coming soon, maybe even a CLI.
data/iosparse.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "iosparse"
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ari Mizrahi"]
9
+ s.date = "2013-05-01"
10
+ s.description = "Parse Cisco IOS configurations"
11
+ s.email = "codemunchies@gmail.com"
12
+ s.files = ["lib/iosparse.rb", "iosparse.gemspec", "Gemfile", "README.md"]
13
+ s.require_paths = ["lib"]
14
+ s.homepage = "http://github.com/codemunchies/iosparse"
15
+ s.rubygems_version = "1.8.25"
16
+ s.summary = "Parse Cisco IOS configurations"
17
+ end
18
+
data/lib/iosparse.rb ADDED
@@ -0,0 +1,113 @@
1
+ class IOSParse
2
+
3
+ #
4
+ # Load the configuration file passing IOSParse the filename
5
+ #
6
+ def initialize(filename)
7
+ # Store all blocks in the configuration
8
+ @blocks = Array.new
9
+ # Load the file
10
+ config = File.open(filename, 'r')
11
+ # Stream IO.read the file and scan for blocks, lines inbetween "!"
12
+ IO.read(config).scan(/(^[^!]+)/).each do |block|
13
+ # Push the blocks to the @blocks array
14
+ @blocks << block
15
+ end
16
+ end
17
+
18
+ #
19
+ # Find the interfaces in the configuration file
20
+ #
21
+ def interfaces
22
+ # Regex to parse out interfaces
23
+ interfaces = /\["interface[\s\S]+/
24
+ # Call findblocks() passing the regex and parent
25
+ findblocks(interfaces, "interface")
26
+ end
27
+
28
+ #
29
+ # Find all names in the configuration file
30
+ #
31
+ def names
32
+ # Regex to parse out names
33
+ names = /\["names[\s\S]+/
34
+ # Call findblocks() passing regex and parent
35
+ findblocks(names, "names")
36
+ end
37
+
38
+ #
39
+ # Find all ACL
40
+ #
41
+ def access_list
42
+ # Regex to parse out acl
43
+ acl = /\["access-list[\s\S]+/
44
+ # Call findblocks() passing regex and parent
45
+ findblocks(acl, "access-list")
46
+ end
47
+
48
+ #
49
+ # Find all objects-groups
50
+ #
51
+ def object_group
52
+ # Regex to parse out object-groups
53
+ group = /object-group[\s\S]+/
54
+ # Call findblocks() passing regex and parent
55
+ findblocks(group, "object-group")
56
+ end
57
+
58
+ private
59
+
60
+ #
61
+ # Primary method to accept regex and parent, then use them to parse blocks and store them in an Array
62
+ #
63
+ def findblocks(filter, parent)
64
+ # Array to store parsed blocks
65
+ output = Array.new
66
+ @blocks.each do |block|
67
+ # Use regex to filter via scan() and flatten then push to array
68
+ output << block.to_s.scan(filter).flatten if block.to_s.include?(parent)
69
+ end
70
+ # Object-groups are not within "!" blocks and require some extra work
71
+ if parent == "object-group" then
72
+ parse_object_group(clean_object_group(output))
73
+ else
74
+ # Return the parsed block as an array
75
+ output
76
+ end
77
+ end
78
+
79
+ #
80
+ # Remove extra characters from object-groups
81
+ #
82
+ def clean_object_group(block)
83
+ # Array to store parsed blocks
84
+ output = Array.new
85
+ block.each do |line|
86
+ # Remove extra "] characters from each line
87
+ output << line.to_s.gsub('\"]', '')
88
+ end
89
+ # Return the parsed block as an array
90
+ output
91
+ end
92
+
93
+ #
94
+ # Object-groups come as a single string, we break each object-group into a string and push into an array
95
+ #
96
+ def parse_object_group(block)
97
+ # Array to store parsed blocks
98
+ output = Array.new
99
+ block.each do |line|
100
+ # Split the line where each object-group begins
101
+ line.to_s.split("object-group").each do |push|
102
+ # Skip empty lines
103
+ next if push.include?('["')
104
+ # Remove extra "] characters from each line
105
+ push = push.gsub('"]', '')
106
+ # Re-add object-group to the beginning of the line and push it into the array line-by-line
107
+ output << "[\"object-group#{push}\"]"
108
+ end
109
+ end
110
+ # Return the parsed block as an array
111
+ output
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iosparse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ari Mizrahi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Parse Cisco IOS configurations
15
+ email: codemunchies@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/iosparse.rb
21
+ - iosparse.gemspec
22
+ - Gemfile
23
+ - README.md
24
+ homepage: http://github.com/codemunchies/iosparse
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.25
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Parse Cisco IOS configurations
48
+ test_files: []