arista-eapi 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/arista-eapi.gemspec +26 -0
- data/examples/save_config.rb +27 -0
- data/lib/arista/eapi.rb +32 -0
- data/lib/arista/eapi/format.rb +27 -0
- data/lib/arista/eapi/parser.rb +16 -0
- data/lib/arista/eapi/parser/show.rb +26 -0
- data/lib/arista/eapi/request.rb +30 -0
- data/lib/arista/eapi/response.rb +75 -0
- data/lib/arista/eapi/switch.rb +32 -0
- data/lib/arista/eapi/version.rb +5 -0
- metadata +125 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mark Imbriaco
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Arista::EAPI
|
2
|
+
|
3
|
+
This gem is designed to make it easy to interact with the Command API that
|
4
|
+
was introduced by Arista Networks in EOS version 4.12.0.
|
5
|
+
|
6
|
+
This is very much an early work in progress. Patches welcome.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'arista-eapi'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install arista-eapi
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
The primary method for interacting with the gem is via the ```Arista::EAPI::Switch#run``` method.
|
25
|
+
|
26
|
+
```
|
27
|
+
switch = Arista::EAPI::Switch.new(hostname, user, password)
|
28
|
+
switch.version
|
29
|
+
#=>
|
30
|
+
{:model_name=>"DCS-7048T-A-R", :internal_version=>"4.12.0-1244071.EOS4120", :system_mac_address=>"00:1c:73:16:c2:c8", :serial_number=>"redacted", :mem_total=>4009152, :bootup_timestamp=>1368735672.690161, :mem_free=>1848284, :version=>"4.12.0", :architecture=>"i386", :internal_build_id=>"c25ec8ea-cb8f-40a8-af0b-d11eaa94d57c", :hardware_revision=>"01.04"}
|
31
|
+
```
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/arista-eapi.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'arista/eapi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "arista-eapi"
|
8
|
+
spec.version = Arista::EAPI::VERSION
|
9
|
+
spec.authors = ["Mark Imbriaco"]
|
10
|
+
spec.email = ["mark@imbriaco.com"]
|
11
|
+
spec.description = %q{Client for Arista Networks eAPI}
|
12
|
+
spec.summary = %q{Arista eAPI Client}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency "rest-client"
|
25
|
+
spec.add_dependency 'json', '~> 1.7.7'
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'arista/eapi'
|
4
|
+
|
5
|
+
user = ENV['EAPI_USER']
|
6
|
+
password = ENV['EAPI_PASSWORD']
|
7
|
+
all = %w(switch1.example.com switch2.example.com)
|
8
|
+
|
9
|
+
all.each do |hostname|
|
10
|
+
print "Saving configuration for #{hostname} ... "
|
11
|
+
|
12
|
+
outdir = File.join(File.dirname(__FILE__), hostname)
|
13
|
+
File.directory?(outdir) || FileUtils.mkdir(outdir)
|
14
|
+
|
15
|
+
sw = Arista::EAPI::Switch.new(hostname, user, password)
|
16
|
+
results = sw.run('enable', 'show running-config')
|
17
|
+
|
18
|
+
File.open(File.join(outdir, 'startup-config'), 'w') do |f|
|
19
|
+
results.each do |res|
|
20
|
+
next if res[:output] == ''
|
21
|
+
f.puts res[:output]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
puts "done."
|
26
|
+
end
|
27
|
+
|
data/lib/arista/eapi.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
require 'cgi'
|
3
|
+
require 'json'
|
4
|
+
require 'restclient'
|
5
|
+
|
6
|
+
require 'arista/eapi/format'
|
7
|
+
require 'arista/eapi/parser'
|
8
|
+
require 'arista/eapi/parser/show'
|
9
|
+
require 'arista/eapi/request'
|
10
|
+
require 'arista/eapi/response'
|
11
|
+
require 'arista/eapi/switch'
|
12
|
+
require 'arista/eapi/version'
|
13
|
+
|
14
|
+
module Arista
|
15
|
+
module EAPI
|
16
|
+
# Public: Determine the eAPI supported format based on the commands.
|
17
|
+
#
|
18
|
+
# options - List of commands to check format options for.
|
19
|
+
#
|
20
|
+
# Examples
|
21
|
+
#
|
22
|
+
# Arista::EAPI.format_for('show vlan')
|
23
|
+
# #=> 'json'
|
24
|
+
# Arista::EAPI.format_for('show vlan', 'show lldp neighbors')
|
25
|
+
# #=> text
|
26
|
+
#
|
27
|
+
# Returns json if all commands support JSON output, otherwise returns text.
|
28
|
+
def self.format_for(*commands)
|
29
|
+
(commands.flatten - Arista::EAPI::Format::JSON_COMMANDS).size > 0 ? 'text' : 'json'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Arista::EAPI::Format
|
2
|
+
JSON_COMMANDS = [
|
3
|
+
'show aliases',
|
4
|
+
'show interfaces',
|
5
|
+
'show interfaces counters',
|
6
|
+
'show interfaces counters discards',
|
7
|
+
'show interfaces status',
|
8
|
+
'show interfaces switchport vlan mapping',
|
9
|
+
'show ip interface',
|
10
|
+
'show ipv6 interface',
|
11
|
+
'show mac address-table',
|
12
|
+
'show mac address-table aging-time',
|
13
|
+
'show management api http-commands',
|
14
|
+
'show management api http-commands certificate',
|
15
|
+
'show monitor server-failure history',
|
16
|
+
'show monitor server-failure servers',
|
17
|
+
'show monitor session',
|
18
|
+
'show privilege',
|
19
|
+
'show sflow',
|
20
|
+
'show sflow interfaces',
|
21
|
+
'show spanning-tree topology status',
|
22
|
+
'show version',
|
23
|
+
'show version license',
|
24
|
+
'show vlan',
|
25
|
+
'show vlan trunk group',
|
26
|
+
]
|
27
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Arista::EAPI::Parser
|
2
|
+
def self.parse(command, body)
|
3
|
+
parser_class = command.split.first.downcase.capitalize
|
4
|
+
parser_method = command.gsub(/\s/, '_')
|
5
|
+
|
6
|
+
parser = self.const_get(parser_class)
|
7
|
+
|
8
|
+
if parser.respond_to?(parser_method.to_sym)
|
9
|
+
parser.send(parser_method, body)
|
10
|
+
else
|
11
|
+
body
|
12
|
+
end
|
13
|
+
rescue NameError
|
14
|
+
body
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Arista::EAPI::Parser::Show
|
2
|
+
class << self
|
3
|
+
def show_lldp_neighbors(body)
|
4
|
+
lldp = {
|
5
|
+
:last_update => nil,
|
6
|
+
:ports => {}
|
7
|
+
}
|
8
|
+
|
9
|
+
lldp[:last_update] = body.match(/^Last table change time\s+:\s(?<last_update>.*?)$/m)[:last_update]
|
10
|
+
ports = body.match(/.*Neighbor Port ID\s*TTL\s(?<ports>.*)/m)[:ports]
|
11
|
+
|
12
|
+
unless ports.nil?
|
13
|
+
ports.split("\n").each do |line|
|
14
|
+
name, neighbor_device_id, neighbor_port_id, ttl = line.split
|
15
|
+
lldp[:ports][name] = {
|
16
|
+
:neighbor_device_id => neighbor_device_id,
|
17
|
+
:neighbor_port_id => neighbor_port_id,
|
18
|
+
:ttl => ttl
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
{ :lldp_neighbors => lldp }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Arista::EAPI::Request
|
2
|
+
attr_accessor :switch, :commands
|
3
|
+
|
4
|
+
def initialize(switch, *commands)
|
5
|
+
self.switch = switch
|
6
|
+
self.commands = commands
|
7
|
+
end
|
8
|
+
|
9
|
+
def payload
|
10
|
+
@payload ||= JSON.generate({
|
11
|
+
:jsonrpc => '2.0',
|
12
|
+
:method => 'runCmds',
|
13
|
+
:id => 1,
|
14
|
+
:params => {
|
15
|
+
:version => 1,
|
16
|
+
:cmds => commands,
|
17
|
+
:format => Arista::EAPI.format_for(commands)
|
18
|
+
},
|
19
|
+
})
|
20
|
+
end
|
21
|
+
|
22
|
+
def execute
|
23
|
+
Arista::EAPI::Response.new(commands, RestClient.post(switch.url, payload))
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.execute!(switch, *commands)
|
27
|
+
req = self.new(switch, *commands)
|
28
|
+
req.execute
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Arista
|
2
|
+
module EAPI
|
3
|
+
module Error
|
4
|
+
class GeneralError < Exception ; end
|
5
|
+
class InternalException < Exception ; end
|
6
|
+
class InvalidCommand < Exception; end
|
7
|
+
class TextOnly < Exception; end
|
8
|
+
class IncomptibleCommand < Exception; end
|
9
|
+
class AmbiguousCommand < Exception; end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Response
|
13
|
+
ERROR_CODES = {
|
14
|
+
1000 => Arista::EAPI::Error::GeneralError,
|
15
|
+
1001 => Arista::EAPI::Error::InternalException,
|
16
|
+
1002 => Arista::EAPI::Error::InvalidCommand,
|
17
|
+
1003 => Arista::EAPI::Error::TextOnly,
|
18
|
+
1004 => Arista::EAPI::Error::IncomptibleCommand,
|
19
|
+
1005 => Arista::EAPI::Error::AmbiguousCommand
|
20
|
+
}
|
21
|
+
|
22
|
+
PARSERS = [ Arista::EAPI::Parser::Show ]
|
23
|
+
|
24
|
+
attr_accessor :code, :response, :results
|
25
|
+
|
26
|
+
def initialize(commands, body)
|
27
|
+
self.response = JSON.parse(body)
|
28
|
+
self.results = []
|
29
|
+
|
30
|
+
if response['error']
|
31
|
+
code = response['error']['code']
|
32
|
+
raise code < 0 ? Exception.new(response['message']) : ERROR_CODES[code]
|
33
|
+
end
|
34
|
+
|
35
|
+
commands.each_with_index do |cmd, idx|
|
36
|
+
results << process_result(cmd, response['result'][idx])
|
37
|
+
end
|
38
|
+
|
39
|
+
results
|
40
|
+
end
|
41
|
+
|
42
|
+
def process_result(cmd, result)
|
43
|
+
case Arista::EAPI.format_for(cmd)
|
44
|
+
when 'json' then
|
45
|
+
convert_hash_keys(result)
|
46
|
+
else
|
47
|
+
Arista::EAPI::Parser.parse(cmd, result['output'])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def symbolize(obj)
|
54
|
+
obj.is_a?(Symbol) ? obj :
|
55
|
+
obj.
|
56
|
+
gsub(/\//, '_').
|
57
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
58
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
59
|
+
downcase.
|
60
|
+
to_sym
|
61
|
+
end
|
62
|
+
|
63
|
+
def convert_hash_keys(value)
|
64
|
+
case value
|
65
|
+
when Array
|
66
|
+
value.map { |v| convert_hash_keys(v) }
|
67
|
+
when Hash
|
68
|
+
Hash[value.map { |k, v| [symbolize(k), convert_hash_keys(v)] }]
|
69
|
+
else
|
70
|
+
value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Arista::EAPI::Switch
|
2
|
+
attr_accessor :hostname, :user, :password, :protocol, :url, :attributes
|
3
|
+
|
4
|
+
def initialize(hostname, user, password, protocol = 'https')
|
5
|
+
self.attributes = {}
|
6
|
+
self.hostname = hostname
|
7
|
+
self.user = user
|
8
|
+
self.password = password
|
9
|
+
self.protocol = protocol
|
10
|
+
|
11
|
+
userpass = [ CGI.escape(user), CGI.escape(password) ].join(':')
|
12
|
+
self.url = "#{protocol}://#{userpass}@#{hostname}/command-api"
|
13
|
+
end
|
14
|
+
|
15
|
+
def interfaces
|
16
|
+
run('show interfaces')
|
17
|
+
attributes[:interfaces]
|
18
|
+
end
|
19
|
+
|
20
|
+
def version
|
21
|
+
run('show version').first
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_attributes!(results)
|
25
|
+
results.each { |result| attributes.merge!(result) if result.is_a?(Hash) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def run(*commands)
|
29
|
+
request = Arista::EAPI::Request.new(self, *commands)
|
30
|
+
self.update_attributes!(request.execute.results)
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arista-eapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.10.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Imbriaco
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rest-client
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: json
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.7.7
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.7.7
|
78
|
+
description: Client for Arista Networks eAPI
|
79
|
+
email:
|
80
|
+
- mark@imbriaco.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- arista-eapi.gemspec
|
91
|
+
- examples/save_config.rb
|
92
|
+
- lib/arista/eapi.rb
|
93
|
+
- lib/arista/eapi/format.rb
|
94
|
+
- lib/arista/eapi/parser.rb
|
95
|
+
- lib/arista/eapi/parser/show.rb
|
96
|
+
- lib/arista/eapi/request.rb
|
97
|
+
- lib/arista/eapi/response.rb
|
98
|
+
- lib/arista/eapi/switch.rb
|
99
|
+
- lib/arista/eapi/version.rb
|
100
|
+
homepage: ''
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 1.8.23
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: Arista eAPI Client
|
125
|
+
test_files: []
|