ssh-shell 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.swp
2
+ pkg
3
+ doc
4
+ Gemfile.lock
data/.hound.yml ADDED
@@ -0,0 +1,35 @@
1
+ ---
2
+ StringLiterals:
3
+ EnforcedStyle: single_quotes
4
+ Enabled: true
5
+
6
+ DotPosition:
7
+ Description: 'Checks the position of the dot in multi-line method calls.'
8
+ EnforcedStyle: leading
9
+ Enabled: true
10
+
11
+ Documentation:
12
+ Description: 'Document classes and non-namespace modules.'
13
+ Enabled: false
14
+
15
+ Encoding:
16
+ EnforcedStyle: when_needed
17
+
18
+ ClassAndModuleChildren:
19
+ Enabled: false
20
+
21
+ TrivialAccessors:
22
+ Enabled: false
23
+
24
+ LineLength:
25
+ Description: 'Limit lines to 137 characters.'
26
+ Max: 120
27
+
28
+ MethodLength:
29
+ Description: 'Avoid methods longer than 10 lines of code.'
30
+ Max: 15
31
+
32
+ ClassLength:
33
+ Description: 'Avoid classes longer than 100 lines of code.'
34
+ CountComments: false # count full line comments?
35
+ Max: 137
data/.rubocop.yml ADDED
@@ -0,0 +1 @@
1
+ inherit_from: .hound.yml
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ ## 0.3.0 (unreleased)
2
+
3
+ - Loosen dependency on net-ssh version
4
+
5
+ ## 0.2.0 (June 13, 2011)
6
+
7
+ - Loosen regex on `on_stdout` [GH-1]
8
+ - Capture stderr from shell. This won't capture the stderr of the processes
9
+ running. Instead it will just capture any error output from the shell process.
10
+ - Ability to specify the default process class to use for `Shell#execute`
11
+
12
+ ## 0.1.0 (October 24, 2010)
13
+
14
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in foreplay.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Net::SSH::Shell
2
+
3
+ ![Gem Version](http://img.shields.io/gem/v/ssh-shell.svg?style=flat) [![Code Climate](http://img.shields.io/codeclimate/github/Xenapto/ssh-shell.svg?style=flat)](https://codeclimate.com/github/Xenapto/ssh-shell)
4
+ [![Developer status](http://img.shields.io/badge/developer-awesome-brightgreen.svg?style=flat)](http://xenapto.com)
5
+
6
+ ## DESCRIPTION:
7
+
8
+ Net::SSH::Shell is a library for interacting with stateful (e.g., interactive) shells on remote hosts. It hides (or tries to hide) the potentially complex Net::SSH state machines you'd otherwise need to write to interact with "su" and similar shells.
9
+
10
+ One of the significant benefits you get from this library versus using Net::SSH directly is that your shell is _stateful_. With Net::SSH, if you invoke "cd /" in one exec call, and "pwd" in another, the two are done in different shells so the directory change in the first has no effect on the working directory of the second. With Net::SSH::Shell, though, commands are all invoked via the _same_ shell, so changes in directory or additions to the environment all affect subsequent commands.
11
+
12
+ ## FEATURES:
13
+
14
+ * Interact with login shells
15
+ * Support for "subshell" environments, like "su" or "sudo bash"
16
+
17
+ ## SYNOPSIS:
18
+
19
+ In a nutshell:
20
+
21
+ ```ruby
22
+ require 'net/ssh'
23
+ require 'net/ssh/shell'
24
+
25
+ Net::SSH::start('host', 'user') do |ssh|
26
+ ssh.shell do |sh|
27
+ sh.execute "cd /usr/local"
28
+ sh.execute "pwd"
29
+ sh.execute "export FOO=bar"
30
+ sh.execute "echo $FOO"
31
+ p=sh.execute "grep dont /tmp/notexist"
32
+ puts "Exit Status:#{p.exit_status}"
33
+ puts "Command Executed:#{p.command}"
34
+ end
35
+ end
36
+ ```
37
+
38
+ See Net::SSH::Shell for more documentation.
39
+
40
+ ## REQUIREMENTS:
41
+
42
+ * net-ssh (version 2)
43
+
44
+ If you want to use any of the Rake tasks, you'll need:
45
+
46
+ * Echoe (for the Rakefile)
47
+
48
+ ## INSTALL:
49
+
50
+ This gem is available from RubyGems, so you can install it using the "gem" command:
51
+
52
+ * gem install net-ssh-shell
53
+
54
+ If you'd like to build the gem for yourself from source:
55
+ * `git clone http://github.com/Xenapto/ssh-shell.git`
56
+ * `cd ssh-shell`
57
+ * `gem install echoe`
58
+ * `rake gem`
59
+ * `gem install pkg/net-ssh-shell-0.1.0.gem` (might need sudo privileges)
60
+
61
+ ## LICENSE:
62
+
63
+ (The MIT License)
64
+
65
+ Copyright (c) 2009 Jamis Buck <jamis@37signals.com>
66
+
67
+ Permission is hereby granted, free of charge, to any person obtaining
68
+ a copy of this software and associated documentation files (the
69
+ 'Software'), to deal in the Software without restriction, including
70
+ without limitation the rights to use, copy, modify, merge, publish,
71
+ distribute, sublicense, and/or sell copies of the Software, and to
72
+ permit persons to whom the Software is furnished to do so, subject to
73
+ the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be
76
+ included in all copies or substantial portions of the Software.
77
+
78
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
79
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
80
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
81
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
82
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
83
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
84
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,185 @@
1
+ require 'digest/sha1'
2
+ require 'net/ssh'
3
+ require 'net/ssh/shell/process'
4
+ require 'net/ssh/shell/subshell'
5
+
6
+ class Net::SSH::Shell
7
+ attr_reader :session
8
+ attr_reader :channel
9
+ attr_reader :state
10
+ attr_reader :shell
11
+ attr_reader :processes
12
+ attr_accessor :default_process_class
13
+
14
+ def initialize(session, shell = :default)
15
+ @session = session
16
+ @shell = shell
17
+ @state = :closed
18
+ @processes = []
19
+ @when_open = []
20
+ @on_process_run = nil
21
+ @default_process_class = Net::SSH::Shell::Process
22
+ open
23
+ end
24
+
25
+ def open(&callback)
26
+ if closed?
27
+ @state = :opening
28
+ @channel = session.open_channel(&method(:open_succeeded))
29
+ @channel.on_open_failed(&method(:open_failed))
30
+ @channel.on_request('exit-status', &method(:on_exit_status))
31
+ end
32
+ when_open(&callback) if callback
33
+ self
34
+ end
35
+
36
+ def open!
37
+ unless open?
38
+ open if closed?
39
+ session.loop { opening? }
40
+ end
41
+ self
42
+ end
43
+
44
+ def when_open(&callback)
45
+ if open?
46
+ yield self
47
+ else
48
+ @when_open << callback
49
+ end
50
+ self
51
+ end
52
+
53
+ def open?
54
+ state == :open
55
+ end
56
+
57
+ def closed?
58
+ state == :closed
59
+ end
60
+
61
+ def opening?
62
+ !open? && !closed?
63
+ end
64
+
65
+ def on_process_run(&callback)
66
+ @on_process_run = callback
67
+ end
68
+
69
+ def execute(command, *args, &callback)
70
+ # The class is an optional second argument.
71
+ klass = default_process_class
72
+ klass = args.shift if args.first.is_a?(Class)
73
+
74
+ # The properties are expected to be the next argument.
75
+ props = {}
76
+ props = args.shift if args.first.is_a?(Hash)
77
+
78
+ process = klass.new(self, command, props, callback)
79
+ processes << process
80
+ run_next_process if processes.length == 1
81
+ process
82
+ end
83
+
84
+ def subshell(command, &callback)
85
+ execute(command, Net::SSH::Shell::Subshell, &callback)
86
+ end
87
+
88
+ def execute!(command, &callback)
89
+ process = execute(command, &callback)
90
+ wait!
91
+ process
92
+ end
93
+
94
+ def busy?
95
+ opening? || processes.any?
96
+ end
97
+
98
+ def wait!
99
+ session.loop { busy? }
100
+ end
101
+
102
+ def close!
103
+ channel.close if channel
104
+ end
105
+
106
+ def child_finished(child)
107
+ channel.on_close(&method(:on_channel_close)) unless channel.nil?
108
+ processes.delete(child)
109
+ run_next_process
110
+ end
111
+
112
+ def separator
113
+ @separator ||= begin
114
+ s = Digest::SHA1.hexdigest(
115
+ [session.object_id, object_id, Time.now.to_i, Time.now.usec, rand(0xFFFFFFFF)].join(':')
116
+ )
117
+
118
+ s << Digest::SHA1.hexdigest(s)
119
+ end
120
+ end
121
+
122
+ def on_channel_close(_channel)
123
+ @state = :closed
124
+ @channel = nil
125
+ end
126
+
127
+ private
128
+
129
+ def run_next_process
130
+ return unless processes.any?
131
+ process = processes.first
132
+ @on_process_run.call(self, process) if @on_process_run
133
+ process.run
134
+ end
135
+
136
+ def open_succeeded(channel)
137
+ @state = :pty
138
+ channel.on_close(&method(:on_channel_close))
139
+ channel.request_pty(modes: { Net::SSH::Connection::Term::ECHO => 0 }, &method(:pty_requested))
140
+ end
141
+
142
+ def open_failed(_channel, code, description)
143
+ @state = :closed
144
+ fail "could not open channel for process manager (#{description}, ##{code})"
145
+ end
146
+
147
+ def on_exit_status(_channel, data)
148
+ fail 'the shell exited unexpectedly' unless data.read_long == 0
149
+ end
150
+
151
+ def pty_requested(channel, success)
152
+ @state = :shell
153
+ fail 'could not request pty for process manager' unless success
154
+ if shell == :default
155
+ channel.send_channel_request('shell', &method(:shell_requested))
156
+ else
157
+ channel.exec(shell, &method(:shell_requested))
158
+ end
159
+ end
160
+
161
+ def shell_requested(channel, success)
162
+ @state = :initializing
163
+ fail 'could not request shell for process manager' unless success
164
+ channel.on_data(&method(:look_for_initialization_done))
165
+ channel.send_data "export PS1=; echo #{separator} $?\n"
166
+ end
167
+
168
+ def look_for_initialization_done(_channel, data)
169
+ return unless data.include?(separator)
170
+ @state = :open
171
+ @when_open.each { |callback| callback.call(self) }
172
+ @when_open.clear
173
+ end
174
+ end
175
+
176
+ class Net::SSH::Connection::Session
177
+ # Provides a convenient way to initialize a shell given a Net::SSH
178
+ # session. Yields the new shell if a block is given. Returns the shell
179
+ # instance.
180
+ def shell(*args)
181
+ shell = Net::SSH::Shell.new(self, *args)
182
+ yield shell if block_given?
183
+ shell
184
+ end
185
+ end
@@ -0,0 +1,120 @@
1
+ class Net::SSH::Shell
2
+ class Process
3
+ attr_reader :state
4
+ attr_reader :command
5
+ attr_reader :manager
6
+ attr_reader :callback
7
+ attr_reader :exit_status
8
+ attr_reader :properties
9
+
10
+ def initialize(manager, command, properties, callback)
11
+ @command = command
12
+ @manager = manager
13
+ @callback = callback
14
+ @properties = properties
15
+ @on_output = nil
16
+ @on_error_output = nil
17
+ @on_finish = nil
18
+ @state = :new
19
+ end
20
+
21
+ def [](key)
22
+ @properties[key]
23
+ end
24
+
25
+ def []=(key, value)
26
+ @properties[key] = value
27
+ end
28
+
29
+ def send_data(data)
30
+ manager.channel.send_data(data)
31
+ end
32
+
33
+ def run
34
+ if state == :new
35
+ state = :starting
36
+ manager.open do
37
+ state = :running
38
+ manager.channel.on_data(&method(:on_stdout))
39
+ manager.channel.on_extended_data(&method(:on_stderr))
40
+ manager.channel.on_close(&method(:on_close))
41
+
42
+ callback.call(self) if callback
43
+
44
+ cmd = command.dup
45
+ cmd << ';' if cmd !~ /[;&]$/
46
+ cmd << " DONTEVERUSETHIS=$?; echo #{manager.separator} $DONTEVERUSETHIS; echo \"exit $DONTEVERUSETHIS\"|sh"
47
+
48
+ send_data(cmd + "\n")
49
+ end
50
+ end
51
+
52
+ self
53
+ end
54
+
55
+ def starting?
56
+ state == :starting
57
+ end
58
+
59
+ def running?
60
+ state == :running
61
+ end
62
+
63
+ def finished?
64
+ state == :finished
65
+ end
66
+
67
+ def busy?
68
+ starting? || running?
69
+ end
70
+
71
+ def wait!
72
+ manager.session.loop { busy? }
73
+ self
74
+ end
75
+
76
+ def on_output(&callback)
77
+ @on_output = callback
78
+ end
79
+
80
+ def on_error_output(&callback)
81
+ @on_error_output = callback
82
+ end
83
+
84
+ def on_finish(&callback)
85
+ @on_finish = callback
86
+ end
87
+
88
+ protected
89
+
90
+ def output!(data)
91
+ @on_output.call(self, data) if @on_output
92
+ end
93
+
94
+ def on_stdout(_ch, data)
95
+ if data.strip =~ /#{manager.separator} (\d+)$/
96
+ before = $`
97
+ output!(before) unless before.empty?
98
+ finished!(Regexp.last_match[1])
99
+ else
100
+ output!(data)
101
+ end
102
+ end
103
+
104
+ def on_stderr(_ch, _type, data)
105
+ @on_error_output.call(self, data) if @on_error_output
106
+ end
107
+
108
+ def on_close(ch)
109
+ manager.on_channel_close(ch)
110
+ finished!(-1)
111
+ end
112
+
113
+ def finished!(status)
114
+ @state = :finished
115
+ @exit_status = status.to_i
116
+ @on_finish.call(self) if @on_finish
117
+ manager.child_finished(self)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,19 @@
1
+ require 'net/ssh/shell/process'
2
+
3
+ class Net::SSH::Shell
4
+ class Subshell < Process
5
+ protected
6
+
7
+ def on_stdout(ch, data)
8
+ return if output!(data)
9
+ ch.on_data(&method(:look_for_finalize_initializer))
10
+ ch.send_data("export PS1=; echo #{manager.separator} $?\n")
11
+ end
12
+
13
+ def look_for_finalize_initializer(ch, data)
14
+ return unless data =~ /#{manager.separator} (\d+)/
15
+ ch.on_close(&@master_onclose)
16
+ finished!(Regexp.last_match[1])
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Net
2
+ module SSH
3
+ class Shell
4
+ VERSION = '0.4.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Net
2
+ module SSH
3
+ class Shell
4
+ VERSION = '0.4.1'
5
+ end
6
+ end
7
+ end
data/ssh-shell.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ssh_shell/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'ssh-shell'
7
+ s.version = Net::SSH::Shell::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Jamis Buck']
10
+ s.email = ['jamis@jamisbuck.org']
11
+ s.homepage = 'http://github.com/Xenapto/ssh-shell'
12
+ s.description = 'A simple library to aid with stateful shell interactions'
13
+ s.summary = 'Updated version based on net-ssh-shell gem'
14
+ s.license = 'MIT'
15
+
16
+ s.add_runtime_dependency 'net-ssh', '~> 2'
17
+
18
+ s.add_development_dependency 'gem-release', '~> 0.7'
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.executables = `git ls-files`.split("\n").map { |f| f =~ /^bin\/(.*)/ ? Regexp.last_match[1] : nil }.compact
22
+ s.require_path = 'lib'
23
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssh-shell
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jamis Buck
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ssh
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: gem-release
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.7'
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.7'
46
+ description: A simple library to aid with stateful shell interactions
47
+ email:
48
+ - jamis@jamisbuck.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .hound.yml
55
+ - .rubocop.yml
56
+ - CHANGELOG.md
57
+ - Gemfile
58
+ - README.md
59
+ - Rakefile
60
+ - lib/net/ssh/shell.rb
61
+ - lib/net/ssh/shell/process.rb
62
+ - lib/net/ssh/shell/subshell.rb
63
+ - lib/net/ssh/shell/version.rb
64
+ - lib/ssh_shell/version.rb
65
+ - ssh-shell.gemspec
66
+ homepage: http://github.com/Xenapto/ssh-shell
67
+ licenses:
68
+ - MIT
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.28
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Updated version based on net-ssh-shell gem
91
+ test_files: []
92
+ has_rdoc: