commutateurs 0.0.1

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.
@@ -0,0 +1,86 @@
1
+ module Commutateurs
2
+ class Credentials
3
+ attr_reader :user, :password, :enable
4
+ def initialize(user, password, enable)
5
+ @user = user
6
+ @password = password
7
+ @enable = enable
8
+ end
9
+ end
10
+
11
+ class Base
12
+ def initialize(host, credentials)
13
+ @enable = credentials.enable
14
+
15
+ @transport = Ssh.new
16
+ @transport.host = host
17
+ @transport.user = credentials.user
18
+ @transport.password = credentials.password
19
+ end
20
+
21
+ def execute(line)
22
+ @transport.command line
23
+ end
24
+ end
25
+
26
+ class Cisco < Base
27
+ def initialize(host, credentials)
28
+ super
29
+ @transport.default_prompt = /[#>]\s?\z/n
30
+ end
31
+
32
+ def enable
33
+ @transport.command("enable", :prompt => /^Password:/)
34
+ @transport.command(@enable)
35
+ end
36
+
37
+ def connect
38
+ @transport.connect
39
+ @transport.command('terminal length 0')
40
+ end
41
+
42
+ def configuration
43
+ execute('show run')
44
+ end
45
+
46
+ def save
47
+ execute('wr mem')
48
+ end
49
+
50
+ def disconnect
51
+ @transport.send 'exit'
52
+ @transport.close
53
+ end
54
+ end
55
+
56
+ class H3c < Base
57
+ def initialize(host, credentials)
58
+ super
59
+ @transport.default_prompt = /(<.*>|\[.*\])$/
60
+ end
61
+
62
+ def enable
63
+ @transport.command('super', :prompt => /Password:/)
64
+ @transport.command(@enable)
65
+ end
66
+
67
+ def connect
68
+ @transport.connect
69
+ end
70
+
71
+ def configuration
72
+ execute('dis curr')
73
+ end
74
+
75
+ def save
76
+ @transport.command('save safely', :prompt => /Are you sure/)
77
+ @transport.command('Y', :prompt => /enter key/)
78
+ @transport.command('')
79
+ end
80
+
81
+ def disconnect
82
+ @transport.send 'quit'
83
+ @transport.close
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,108 @@
1
+ # This code is nearly the same as the code below. Thanks to Puppet network device authors and net-ssh-telnet authors
2
+ # https://github.com/puppetlabs/puppet/blob/master/lib/puppet/util/network_device/transport/ssh.rb
3
+ module Commutateurs
4
+ class Ssh
5
+ attr_accessor :user, :password, :host, :port
6
+ attr_accessor :default_prompt, :timeout
7
+
8
+ def initialize(verbose = false)
9
+ @timeout = 10
10
+ @verbose = verbose
11
+ end
12
+
13
+ def command(cmd, options = {})
14
+ send(cmd)
15
+ expect(options[:prompt] || default_prompt) do |output|
16
+ yield output if block_given?
17
+ end
18
+ end
19
+
20
+ def connect(&block)
21
+ @output = []
22
+ @channel_data = ""
23
+
24
+ @ssh = Net::SSH.start(host, user, :port => port, :password => password, :timeout => timeout)
25
+
26
+ @buf = ""
27
+ @eof = false
28
+ @channel = nil
29
+ @ssh.open_channel do |channel|
30
+ channel.request_pty { |ch,success| raise "failed to open pty" unless success }
31
+
32
+ channel.send_channel_request("shell") do |ch, success|
33
+ raise "failed to open ssh shell channel" unless success
34
+
35
+ ch.on_data { |ch,data|
36
+ $stderr.print data if @verbose
37
+ @buf << data
38
+ }
39
+ ch.on_extended_data { |ch,type,data| $stderr.print data if type == 1; @buf << data if type == 1 }
40
+ ch.on_close { @eof = true }
41
+
42
+ @channel = ch
43
+ expect(default_prompt, &block)
44
+ return
45
+ end
46
+
47
+ end
48
+ @ssh.loop
49
+ end
50
+
51
+ def close
52
+ Timeout::timeout(2) {
53
+ @channel.close if @channel
54
+ @channel = nil
55
+ @ssh.close if @ssh
56
+ }
57
+ rescue Timeout::Error
58
+ end
59
+
60
+ def expect(prompt)
61
+ line = ''
62
+ sock = @ssh.transport.socket
63
+
64
+ while not @eof
65
+ break if line =~ prompt and @buf == ''
66
+ break if sock.closed?
67
+
68
+ IO::select([sock], [sock], nil, nil)
69
+
70
+ process_ssh
71
+
72
+ if @buf != ""
73
+ line += @buf.gsub(/\r\n/no, "\n")
74
+ @buf = ''
75
+ yield line if block_given?
76
+ elsif @eof
77
+ # channel has been closed
78
+ break if line =~ prompt
79
+ if line == ''
80
+ line = nil
81
+ yield nil if block_given?
82
+ end
83
+ break
84
+ end
85
+ end
86
+ line
87
+ end
88
+
89
+ def send(line)
90
+ @channel.send_data(line + "\n")
91
+ end
92
+
93
+ private
94
+ def eof?
95
+ !! @eof
96
+ end
97
+
98
+ def process_ssh
99
+ while @buf == "" and not eof?
100
+ begin
101
+ @channel.connection.process(0.1)
102
+ rescue IOError
103
+ @eof = true
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,9 @@
1
+ require 'timeout'
2
+ require 'net/ssh'
3
+
4
+ module Commutateurs
5
+ end
6
+
7
+ require 'commutateurs/ssh'
8
+ require 'commutateurs/device'
9
+
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commutateurs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Guillaume Rose
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-01-01 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: net-ssh
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Library for accessing commutateurs
35
+ email: guillaume.rose@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/commutateurs/device.rb
44
+ - lib/commutateurs/ssh.rb
45
+ - lib/commutateurs.rb
46
+ homepage: http://www.github.com/guillaumerose/commutateurs
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.15
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Library for accessing commutateurs
79
+ test_files: []
80
+