dumbcli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6791be747b4c86c5138b4cbbd24e9b05e6fe3ee6
4
+ data.tar.gz: 015556c2860acfe1483d8a43b410bede61aa9e38
5
+ SHA512:
6
+ metadata.gz: a267c62ac02053fc5111a6d6a6faec25fe489d52d6a3c59c304a4fcc08ca7662ecfcf1ed3d7a331056fc4abff21b88bcf0fd5ccc2c5d9f3a47e66e62d8b4dc48
7
+ data.tar.gz: ff52eb14a2ec11c4cf0559bfe2773bf615874b60e9f3d4687e410b6f0423ac920017111ae22ee2dcc2cae438704fcbbb9f74719c5a7f9db8606bea04c7d2f4a0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dumbcli.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Shintaro Kojima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # DumbCLI
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dumbcli`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dumbcli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dumbcli
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/dumbcli/fork )
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 a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dumbcli"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/dumbcli.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dumbcli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dumbcli"
8
+ spec.version = Dumbcli::VERSION
9
+ spec.authors = ["Shintaro Kojima"]
10
+ spec.email = ["goodies@codeout.net"]
11
+
12
+ spec.summary = 'A scaffold of ruby wrapper of rancid xlogin'
13
+ spec.description = "DumbCLI is a ruby wrapper of rancid xlogin that is a CLI handler of network devices. You can easily extends DumbCLI's scaffold classes to manage your devices."
14
+ spec.homepage = 'https://github.com/codeout/dumbcli'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
@@ -0,0 +1,33 @@
1
+ module Dumbcli
2
+ module Device
3
+ class A10 < Base
4
+ def connect
5
+ super
6
+ flush
7
+ execute 'terminal length 0'
8
+ end
9
+
10
+ def execute(cmd)
11
+ super
12
+ end
13
+
14
+
15
+ private
16
+
17
+ def login_cmd
18
+ "#{Dumbcli::Device.login_cmd('cisco')} #{@host}"
19
+ end
20
+
21
+ def execute_exit
22
+ result = execute("exit\nexit\nY")
23
+ sleep 1
24
+ result << flush
25
+
26
+ unless result =~ /connection closed|Connection to \S+ closed\./i
27
+ sleep 1
28
+ execute "exit\nexit\nY"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ module Dumbcli
2
+ module Device
3
+ class Arista < Base
4
+ def connect
5
+ term = ENV['TERM']
6
+ ENV['TERM'] = 'vt100'
7
+
8
+ super
9
+ flush
10
+ execute 'terminal length 0'
11
+
12
+ ENV['TERM'] = term
13
+ end
14
+
15
+
16
+ private
17
+
18
+ def login_cmd
19
+ "#{Dumbcli::Device.login_cmd('arista')} #{@host}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,103 @@
1
+ require 'net/telnet'
2
+
3
+ module Dumbcli
4
+ module Device
5
+ class Base
6
+ def initialize(host, options={})
7
+ raise ArgumentError, 'host is required' unless host
8
+ @host = host
9
+ @options = options
10
+ end
11
+
12
+ def execute(cmd)
13
+ raise ArgumentError, 'cmd is required' unless cmd
14
+
15
+ connect unless established?
16
+
17
+ Dumbcli.logger.info "Executing: #{cmd}"
18
+ @connection.cmd(cmd).tap {|result|
19
+ Dumbcli.logger.debug result
20
+ }
21
+ end
22
+ alias exec execute
23
+
24
+ def established?
25
+ @connection && !@connection.sock.closed?
26
+ end
27
+
28
+ def connect
29
+ return if established?
30
+
31
+ Dumbcli.logger.info "Trying to connect #{@host}"
32
+ @connection = Net::Telnet.new('Prompt' => prompt,
33
+ 'Proxy' => IO.popen(login_cmd, 'r+'),
34
+ 'Timeout' => @options[:timeout] || 30)
35
+
36
+ begin
37
+ result = @connection.waitfor(prompt)
38
+ if result =~ error_code
39
+ @connection.close
40
+ raise $1
41
+ end
42
+ rescue
43
+ @connection.close
44
+ raise
45
+ end
46
+ end
47
+
48
+ def close
49
+ return unless established?
50
+
51
+ execute_exit
52
+ end
53
+
54
+
55
+ private
56
+
57
+ def login_cmd
58
+ raise 'Not implemented yet'
59
+ end
60
+
61
+ def prompt
62
+ p = /\S+(?:>|#)\s*/
63
+ /#{p}(?:[\n\r]+#{p})*/
64
+ end
65
+
66
+ def error_code
67
+ /^Error:\s*(.*)$/
68
+ end
69
+
70
+ def execute_exit
71
+ unless execute('exit') =~ /connection closed|Connection to \S+ closed\./i # see clogin
72
+ sleep 1 # wait a bit until completely close
73
+ execute 'exit' # even from 'configure terminal' mode. see clogin
74
+ end
75
+ end
76
+
77
+ def read
78
+ return unless established?
79
+ result = ''
80
+
81
+ begin
82
+ loop do
83
+ result << @connection.sock.read_nonblock(1024)
84
+ end
85
+ rescue Errno::EAGAIN, Errno::EINTR, EOFError
86
+ end
87
+
88
+ result
89
+ end
90
+
91
+ def flush
92
+ result = ''
93
+ sleep 0.5
94
+
95
+ until (remainder = read).empty?
96
+ result << remainder
97
+ end
98
+
99
+ result
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,22 @@
1
+ module Dumbcli
2
+ module Device
3
+ class Cisco < Base
4
+ def connect
5
+ super
6
+ flush
7
+ execute 'terminal length 0'
8
+ end
9
+
10
+ def execute(cmd)
11
+ super + flush
12
+ end
13
+
14
+
15
+ private
16
+
17
+ def login_cmd
18
+ "#{Dumbcli::Device.login_cmd('cisco')} #{@host}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ module Dumbcli
2
+ module Device
3
+ class CiscoAsa < Base
4
+ def connect
5
+ super
6
+ flush
7
+ execute 'terminal pager 0'
8
+ end
9
+
10
+
11
+ private
12
+
13
+ def login_cmd
14
+ "#{Dumbcli::Device.login_cmd('cisco')} #{@host}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Dumbcli
2
+ module Device
3
+ class CiscoXr < Base
4
+ def connect
5
+ super
6
+ flush
7
+ execute 'terminal length 0'
8
+ end
9
+
10
+
11
+ private
12
+
13
+ def login_cmd
14
+ "#{Dumbcli::Device.login_cmd('cisco-xr')} #{@host}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module Dumbcli
2
+ module Device
3
+ class Juniper < Base
4
+ def connect
5
+ super
6
+ flush
7
+ execute 'set cli screen-length 0'
8
+ end
9
+
10
+ def execute(cmd)
11
+ super + flush
12
+ end
13
+
14
+
15
+ private
16
+
17
+ def login_cmd
18
+ "#{Dumbcli::Device.login_cmd('juniper')} #{@host}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,58 @@
1
+ require 'dumbcli/device/base.rb'
2
+ require 'dumbcli/device/a10.rb'
3
+ require 'dumbcli/device/arista.rb'
4
+ require 'dumbcli/device/cisco.rb'
5
+ require 'dumbcli/device/cisco_asa.rb'
6
+ require 'dumbcli/device/cisco_xr.rb'
7
+ require 'dumbcli/device/juniper.rb'
8
+
9
+ module Dumbcli
10
+ module Device
11
+ def self.login_cmd(platform)
12
+ rancid_types = {
13
+ 'adtran' => 'clogin',
14
+ 'alteon' => 'alogin',
15
+ 'arbor' => 'anlogin',
16
+ 'arista' => 'clogin',
17
+ 'avocent' => 'avologin',
18
+ 'baynet' => 'blogin',
19
+ 'cat5' => 'clogin',
20
+ 'cisco-nx' => 'clogin',
21
+ 'cisco-wlc4' => 'wlogin',
22
+ 'cisco-wlc5' => 'wlogin',
23
+ 'cisco-xr' => 'clogin',
24
+ 'cisco' => 'clogin',
25
+ 'compass' => 'complogin',
26
+ 'css' => 'clogin',
27
+ 'dell' => 'dllogin',
28
+ 'enterasys' => 'rivlogin',
29
+ 'erx' => 'clogin',
30
+ 'extreme' => 'clogin',
31
+ 'ezt3' => 'elogin',
32
+ 'f5' => 'clogin',
33
+ 'force10' => 'clogin',
34
+ 'fortigate' => 'fnlogin',
35
+ 'foundry' => 'flogin',
36
+ 'hitachi' => 'htlogin',
37
+ 'hp' => 'hlogin',
38
+ 'juniper-srx' => 'jlogin',
39
+ 'juniper' => 'jlogin',
40
+ 'mikrotik' => 'mtlogin',
41
+ 'mrtd' => 'clogin',
42
+ 'mrv' => 'mrvlogin',
43
+ 'netopia' => 'tlogin',
44
+ 'netscaler' => 'nslogin',
45
+ 'netscreen' => 'nlogin',
46
+ 'paloalto' => 'panlogin',
47
+ 'procket' => 'clogin',
48
+ 'redback' => 'clogin',
49
+ 'riverstone' => 'rivlogin',
50
+ 'smc' => 'hlogin',
51
+ 'xirrus' => 'xilogin',
52
+ 'zebra' => 'clogin',
53
+ }
54
+
55
+ rancid_types[platform]
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,26 @@
1
+ require 'logger'
2
+
3
+ module Dumbcli
4
+ class Logger < ::Logger
5
+ CLEAR = "\e[0m"
6
+ COLORS = {
7
+ 'INFO' => "\e[32m", # green
8
+ 'WARN' => "\e[33m", # yellow
9
+ 'ERROR' => "\e[35m", # magenta
10
+ 'FATAL' => "\e[31m", # red
11
+ }
12
+
13
+ def initialize(logdev, shift_age = 0, shift_size = 1048576)
14
+ super
15
+ @level = INFO
16
+ end
17
+
18
+ def format_message(severity, datetime, progname, msg)
19
+ if COLORS[severity]
20
+ "#{COLORS[severity]}#{super}#{CLEAR}"
21
+ else
22
+ super
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Dumbcli
2
+ VERSION = '0.0.1'
3
+ end
data/lib/dumbcli.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'dumbcli/device'
2
+ require 'dumbcli/logger'
3
+ require 'dumbcli/version'
4
+
5
+ module Dumbcli
6
+ @logger = Dumbcli::Logger.new(STDOUT).tap {|l| l.level = Dumbcli::Logger::Severity::INFO }
7
+
8
+ class << self
9
+ attr_accessor :logger
10
+
11
+ def Device(host, platform, options={})
12
+ case platform
13
+ when 'a10'
14
+ Dumbcli::Device::A10.new(host, options)
15
+ when 'arista'
16
+ Dumbcli::Device::Arista.new(host, options)
17
+ when 'cisco'
18
+ Dumbcli::Device::Cisco.new(host, options)
19
+ when 'cisco-asa'
20
+ Dumbcli::Device::CiscoAsa.new(host, options)
21
+ when 'cisco-xr'
22
+ Dumbcli::Device::CiscoXr.new(host, options)
23
+ when 'juniper'
24
+ Dumbcli::Device::Juniper.new(host, options)
25
+ else
26
+ raise ArgumentError, %(Unknown device type: "#{platform}" for host "#{host}")
27
+ end
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dumbcli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shintaro Kojima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: DumbCLI is a ruby wrapper of rancid xlogin that is a CLI handler of network
42
+ devices. You can easily extends DumbCLI's scaffold classes to manage your devices.
43
+ email:
44
+ - goodies@codeout.net
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - dumbcli.gemspec
58
+ - lib/dumbcli.rb
59
+ - lib/dumbcli/device.rb
60
+ - lib/dumbcli/device/a10.rb
61
+ - lib/dumbcli/device/arista.rb
62
+ - lib/dumbcli/device/base.rb
63
+ - lib/dumbcli/device/cisco.rb
64
+ - lib/dumbcli/device/cisco_asa.rb
65
+ - lib/dumbcli/device/cisco_xr.rb
66
+ - lib/dumbcli/device/juniper.rb
67
+ - lib/dumbcli/logger.rb
68
+ - lib/dumbcli/version.rb
69
+ homepage: https://github.com/codeout/dumbcli
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.4.5
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A scaffold of ruby wrapper of rancid xlogin
93
+ test_files: []
94
+ has_rdoc: