oxidized-script 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: ea2afb0bc4bb68667bc29704bde3f1a70cb591f4
4
+ data.tar.gz: 0121bfb120faa8af163da27cb0e302324df8811d
5
+ SHA512:
6
+ metadata.gz: d850980e48c31ba44f5618906219ab8b5618e4002aa6d8bf1e240b02f1c8aee4d6bd41c7247e1fb1659d26133d32acf71e3a6d37e628d0adc7be75a7089701c1
7
+ data.tar.gz: ea183301f772e8035bbbe49b38473ccc5a2a4b80994a50273f8c0fa9df0b924cad986b07a289d0b1868069514d2de3bee8660c4361c99817a3aff4db93db8d7c
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ gems
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Oxidized Script
2
+ CLI and Library to interface with network devices in Oxidized
3
+
4
+ ## Install
5
+ % gem install oxidized-script
6
+
7
+ ## Use
8
+
9
+ ### CLI
10
+ ```
11
+ [fisakytt@lan-login1 ~]% oxs S-2250220 'sh ver'
12
+ Jan 29 2010 12:18:24
13
+ K.14.54
14
+ 79
15
+ [fisakytt@lan-login1 ~]% cat > cmds
16
+ show ip route
17
+ [fisakytt@lan-login1 ~]% oxs -x cmds 62.236.123.199
18
+ Default gateway is 62.236.123.198
19
+
20
+ Host Gateway Last Use Total Uses Interface
21
+ ICMP redirect cache is empty
22
+ [fisakytt@lan-login1 ~]% cat >> cmds
23
+ sh ip cef
24
+ [fisakytt@lan-login1 ~]% cat cmds|oxs -x- 62.236.123.199
25
+ Default gateway is 62.236.123.198
26
+
27
+ Host Gateway Last Use Total Uses Interface
28
+ ICMP redirect cache is empty
29
+ %IPv4 CEF not running
30
+ [fisakytt@lan-login1 ~]% oxs --help
31
+ Usage: oxs [options] hostname [command]
32
+ -m, --model host model (ios, junos, etc), otherwise discovered from Oxidized source
33
+ -x, --commands commands file to be sent
34
+ -u, --username username to use
35
+ -p, --password password to use
36
+ -t, --timeout timeout value to use
37
+ -e, --enable enable password to use
38
+ -d, --debug turn on debugging
39
+ -h, --help Display this help message.
40
+ [fisakytt@lan-login1 ~]%
41
+ ```
42
+
43
+ ### Library
44
+ ```
45
+ [fisakytt@lan-login1 ~]% cat moi42.b
46
+ #!/usr/bin/env ruby
47
+
48
+ require 'oxidized/script'
49
+
50
+ Oxidized::Script.new(:host=>'62.236.123.199') do |oxs|
51
+ puts oxs.cmd 'show mac address-table dynamic vlan 101'
52
+ end
53
+ [fisakytt@lan-login1 ~]% ./moi42.b
54
+ Mac Address Table
55
+ -------------------------------------------
56
+
57
+ Vlan Mac Address Type Ports
58
+ ---- ----------- -------- -----
59
+ 101 44d3.ca4c.383e DYNAMIC Gi0/1
60
+ [fisakytt@lan-login1 ~]%
61
+ ```
62
+
63
+ ## TODO
64
+ * Interactive use?
65
+ * Tests+docs, as always :(
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ begin
2
+ require 'rake/testtask'
3
+ require 'bundler'
4
+ Bundler.setup
5
+ rescue LoadError
6
+ warn 'bunler missing'
7
+ exit 42
8
+ end
9
+
10
+ gemspec = eval(File.read(Dir['*.gemspec'].first))
11
+ file = [gemspec.name, gemspec.version].join('-') + '.gem'
12
+
13
+ desc 'Validate gemspec'
14
+ task :gemspec do
15
+ gemspec.validate
16
+ end
17
+
18
+ desc 'Run minitest'
19
+ task :test do
20
+ Rake::TestTask.new do |t|
21
+ t.libs.push "lib"
22
+ t.test_files = FileList['spec/*_spec.rb']
23
+ t.verbose = true
24
+ end
25
+ end
26
+
27
+ desc 'Build gem'
28
+ task :build do
29
+ system "gem build #{gemspec.name}.gemspec"
30
+ FileUtils.mkdir_p 'gems'
31
+ FileUtils.mv file, 'gems'
32
+ end
33
+
34
+ desc 'Install gem'
35
+ task :install => :build do
36
+ system "sudo -E sh -c \'umask 022; gem install gems/#{file}\'"
37
+ end
38
+
39
+ desc 'Remove gems'
40
+ task :clean do
41
+ FileUtils.rm_rf 'gems'
42
+ end
43
+
44
+ desc 'Push to rubygems'
45
+ task :push do
46
+ system "gem push gems/#{file}"
47
+ end
data/bin/oxs ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'oxidized/script/cli'
5
+ puts Oxidized::Script::CLI.new.run
6
+ rescue => error
7
+ warn "#{error}"
8
+ raise if Oxidized::CFG.debug
9
+ end
@@ -0,0 +1 @@
1
+ require_relative 'script/script'
@@ -0,0 +1,67 @@
1
+ module Oxidized
2
+ require_relative 'script'
3
+ require 'slop'
4
+ class Script
5
+ class CLI
6
+ class CLIError < ScriptError; end
7
+ class NothingToDo < ScriptError; end
8
+
9
+ def run
10
+ connect
11
+ if @opts[:commands]
12
+ run_file @opts[:commands]
13
+ elsif @cmd
14
+ @oxs.cmd @cmd
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def initialize
21
+ args, @opts = opts_parse
22
+ CFG.debug = true if @opts[:debug]
23
+ @host = args.shift
24
+ @cmd = args.shift if args
25
+ @oxs = nil
26
+ raise NothingToDo, 'no host given' if not @host
27
+ raise NothingToDo, 'nothing to do, give command or -x' if not @cmd and not @opts[:commands]
28
+ end
29
+
30
+ def opts_parse
31
+ slop = Slop.parse(:help=>true) do
32
+ banner 'Usage: oxs [options] hostname [command]'
33
+ on 'm=', '--model', 'host model (ios, junos, etc), otherwise discovered from Oxidized source'
34
+ on 'x=', '--commands', 'commands file to be sent'
35
+ on 'u=', '--username', 'username to use'
36
+ on 'p=', '--password', 'password to use'
37
+ on 't=', '--timeout', 'timeout value to use'
38
+ on 'e=', '--enable', 'enable password to use'
39
+ on 'd', '--debug', 'turn on debugging'
40
+ end
41
+ [slop.parse!, slop]
42
+ end
43
+
44
+ def connect
45
+ opts = {}
46
+ opts[:host] = @host
47
+ opts[:model] = @opts[:model] if @opts[:model]
48
+ opts[:username] = @opts[:username] if @opts[:username]
49
+ opts[:password] = @opts[:password] if @opts[:password]
50
+ opts[:timeout] = @opts[:timeout].to_i if @opts[:timeout]
51
+ opts[:enable] = @opts[:enable] if @opts[:enable]
52
+ @oxs = Script.new opts
53
+ end
54
+
55
+ def run_file file
56
+ out = ''
57
+ file = file == '-' ? $stdin : File.read(file)
58
+ file.each_line do |line|
59
+ line.sub!(/\\n/, "\n") # tread escaped newline as newline
60
+ out += @oxs.cmd line
61
+ end
62
+ out
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module Oxidized
4
+ require 'oxidized'
5
+ class Script
6
+ class ScriptError < OxidizedError; end
7
+ class NoConnection < ScriptError; end
8
+ class NoNode < ScriptError; end
9
+ class InvalidOption < ScriptError; end
10
+
11
+ # @param [String] command command to be sent
12
+ # @return [String] output for command
13
+ def cmd command
14
+ @model.cmd command
15
+ end
16
+
17
+ # disconnects from ssh/telnet session
18
+ # @return [void]
19
+ def disconnect
20
+ @input.disconnect_cli
21
+ end
22
+ alias_method :close, :disconnect
23
+
24
+ private
25
+
26
+ # @param [Hash] opts options for Oxidized::Script
27
+ # @option opts [String] :host hostname or ip address for Oxidized::Node
28
+ # @option opts [String] :model node model (ios, junos etc) if defined, nodes are not loaded from source
29
+ # @option opts [Fixnum] :timeout oxidized timeout
30
+ # @option opts [String :username username for login
31
+ # @option opts [String :passsword password for login
32
+ # @option opts [String :enable enable password to use
33
+ # @yieldreturn [self] if called in block, returns self and disconnnects session after exiting block
34
+ # @return [void]
35
+ def initialize opts, &block
36
+ host = opts.delete :host
37
+ model = opts.delete :model
38
+ timeout = opts.delete :timeout
39
+ username = opts.delete :username
40
+ password = opts.delete :password
41
+ enable = opts.delete :enable
42
+ raise InvalidOption, "#{opts} not recognized" unless opts.empty?
43
+ Oxidized.mgr = Manager.new
44
+ @node = if model
45
+ Node.new(:name=>host, :model=>model)
46
+ else
47
+ Nodes.new(:node=>host).first
48
+ end
49
+ raise NoNode, 'node not found' unless @node
50
+ @node.auth[:username] = username if username
51
+ @node.auth[:password] = password if password
52
+ CFG.vars[:enable] = enable if enable
53
+ CFG.timeout = timeout if timeout
54
+ @model = @node.model
55
+ @input = nil
56
+ connect
57
+ if block_given?
58
+ yield self
59
+ disconnect
60
+ end
61
+ end
62
+
63
+ def connect
64
+ @node.input.each do |input|
65
+ begin
66
+ @node.model.input = input.new
67
+ @node.model.input.connect @node
68
+ break
69
+ rescue
70
+ end
71
+ end
72
+ @input = @node.model.input
73
+ raise NoConnection, 'unable to connect' unless @input.connected?
74
+ @input.connect_cli
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'oxidized-script'
3
+ s.version = '0.0.1'
4
+ s.platform = Gem::Platform::RUBY
5
+ s.authors = [ 'Saku Ytti' ]
6
+ s.email = %w( saku@ytti.fi )
7
+ s.homepage = 'http://github.com/ytti/oxidized-script'
8
+ s.summary = 'cli + library for scripting network devices'
9
+ s.description = 'rancid clogin-like script to push configs to devices + library interface to do same'
10
+ s.rubyforge_project = s.name
11
+ s.files = `git ls-files`.split("\n")
12
+ s.executables = %w( oxs )
13
+ s.require_path = 'lib'
14
+
15
+ #s.add_dependency 'oxidized'
16
+ s.add_dependency 'slop'
17
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oxidized-script
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Saku Ytti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: slop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: rancid clogin-like script to push configs to devices + library interface
28
+ to do same
29
+ email:
30
+ - saku@ytti.fi
31
+ executables:
32
+ - oxs
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - README.md
39
+ - Rakefile
40
+ - bin/oxs
41
+ - lib/oxidized/script.rb
42
+ - lib/oxidized/script/cli.rb
43
+ - lib/oxidized/script/script.rb
44
+ - oxidized-script.gemspec
45
+ homepage: http://github.com/ytti/oxidized-script
46
+ licenses: []
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: oxidized-script
64
+ rubygems_version: 2.0.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: cli + library for scripting network devices
68
+ test_files: []