net-ssh-shell 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ === (unreleased)
2
+
3
+ * Initial release
data/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ CHANGELOG.rdoc
2
+ lib/net/ssh/shell/process.rb
3
+ lib/net/ssh/shell/subshell.rb
4
+ lib/net/ssh/shell/version.rb
5
+ lib/net/ssh/shell.rb
6
+ Rakefile
7
+ README.rdoc
8
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,79 @@
1
+ = Net::SSH::Shell
2
+
3
+ == DESCRIPTION:
4
+
5
+ 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.
6
+
7
+ 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.
8
+
9
+ == FEATURES:
10
+
11
+ * Interact with login shells
12
+ * Support for "subshell" environments, like "su" or "sudo bash"
13
+
14
+ == SYNOPSIS:
15
+
16
+ In a nutshell:
17
+
18
+ require 'net/ssh'
19
+ require 'net/ssh/shell'
20
+
21
+ Net::SSH::start('host', 'user') do |ssh|
22
+ ssh.shell do |sh|
23
+ sh.execute "cd /usr/local"
24
+ sh.execute "pwd"
25
+ sh.execute "export FOO=bar"
26
+ sh.execute "echo $FOO"
27
+ p=sh.execute "grep dont /tmp/notexist"
28
+ puts "Exit Status:#{p.exit_status}"
29
+ puts "Command Executed:#{p.command}"
30
+ end
31
+ end
32
+
33
+ See Net::SSH::Shell for more documentation.
34
+
35
+ == REQUIREMENTS:
36
+
37
+ * net-ssh (version 2)
38
+
39
+ If you want to use any of the Rake tasks, you'll need:
40
+
41
+ * Echoe (for the Rakefile)
42
+
43
+ == INSTALL:
44
+ This gem is not yet on rubygems , so you have to install it from this source
45
+
46
+ * gem install net-ssh-shell
47
+ ERROR: could not find gem net-ssh-shell locally or in a repository
48
+
49
+ Therefor you need to build the gem yourself:
50
+ * git clone http://github.com/jedi4ever/net-ssh-shell.git
51
+ * cd net-ssh-shell
52
+ * gem install echoe
53
+ * rake gem
54
+ * gem install pkg/net-ssh-shell-0.1.0.gem (might need sudo privileges)
55
+
56
+ == LICENSE:
57
+
58
+ (The MIT License)
59
+
60
+ Copyright (c) 2009 Jamis Buck <jamis@37signals.com>
61
+
62
+ Permission is hereby granted, free of charge, to any person obtaining
63
+ a copy of this software and associated documentation files (the
64
+ 'Software'), to deal in the Software without restriction, including
65
+ without limitation the rights to use, copy, modify, merge, publish,
66
+ distribute, sublicense, and/or sell copies of the Software, and to
67
+ permit persons to whom the Software is furnished to do so, subject to
68
+ the following conditions:
69
+
70
+ The above copyright notice and this permission notice shall be
71
+ included in all copies or substantial portions of the Software.
72
+
73
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
74
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
75
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
76
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
77
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
78
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
79
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require './lib/net/ssh/shell/version'
2
+
3
+ begin
4
+ require 'echoe'
5
+ rescue LoadError
6
+ abort "You'll need to have `echoe' installed to use Net::SSH::Shell's Rakefile"
7
+ end
8
+
9
+ version = Net::SSH::Shell::Version::STRING.dup
10
+ if ENV['SNAPSHOT'].to_i == 1
11
+ version << "." << Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ Echoe.new('net-ssh-shell', version) do |p|
15
+ p.changelog = "CHANGELOG.rdoc"
16
+
17
+ p.author = "Jamis Buck"
18
+ p.email = "jamis@jamisbuck.org"
19
+ p.summary = "A simple library to aid with stateful shell interactions"
20
+ p.url = "http://net-ssh.rubyforge.org/shell"
21
+
22
+ p.dependencies = ["net-ssh >=2.0.9"]
23
+
24
+ p.need_zip = true
25
+ p.include_rakefile = true
26
+
27
+ p.rdoc_pattern = /^(lib|README.rdoc|CHANGELOG.rdoc)/
28
+ end
@@ -0,0 +1,160 @@
1
+ require 'digest/sha1'
2
+ require 'net/ssh'
3
+ require 'net/ssh/shell/process'
4
+ require 'net/ssh/shell/subshell'
5
+
6
+ module Net
7
+ module SSH
8
+ class Shell
9
+ attr_reader :session
10
+ attr_reader :channel
11
+ attr_reader :state
12
+ attr_reader :shell
13
+ attr_reader :processes
14
+
15
+ def initialize(session, shell=:default)
16
+ @session = session
17
+ @shell = shell
18
+ @state = :closed
19
+ @processes = []
20
+ @when_open = []
21
+ open
22
+ end
23
+
24
+ def open(&callback)
25
+ if closed?
26
+ @state = :opening
27
+ @channel = session.open_channel(&method(:open_succeeded))
28
+ @channel.on_open_failed(&method(:open_failed))
29
+ end
30
+ when_open(&callback) if callback
31
+ self
32
+ end
33
+
34
+ def open!
35
+ if !open?
36
+ open if closed?
37
+ session.loop { opening? }
38
+ end
39
+ self
40
+ end
41
+
42
+ def when_open(&callback)
43
+ if open?
44
+ yield self
45
+ else
46
+ @when_open << callback
47
+ end
48
+ self
49
+ end
50
+
51
+ def open?
52
+ state == :open
53
+ end
54
+
55
+ def closed?
56
+ state == :closed
57
+ end
58
+
59
+ def opening?
60
+ !open? && !closed?
61
+ end
62
+
63
+ def execute(command, klass=Net::SSH::Shell::Process, &callback)
64
+ process = klass.new(self, command, callback)
65
+ process.run if processes.empty?
66
+ processes << process
67
+ return process
68
+ end
69
+
70
+ def subshell(command, &callback)
71
+ execute(command, Net::SSH::Shell::Subshell, &callback)
72
+ end
73
+
74
+ def execute!(command, &callback)
75
+ execute(command, &callback)
76
+ wait!
77
+ return process
78
+ end
79
+
80
+ def busy?
81
+ opening? || processes.any?
82
+ end
83
+
84
+ def wait!
85
+ session.loop { busy? }
86
+ end
87
+
88
+ def close!
89
+ channel.close if channel
90
+ end
91
+
92
+ def child_finished(child)
93
+ channel.on_close(&method(:on_channel_close)) if !channel.nil?
94
+ processes.delete(child)
95
+ processes.first.run if processes.any?
96
+ end
97
+
98
+ def separator
99
+ @separator ||= begin
100
+ s = Digest::SHA1.hexdigest([session.object_id, object_id, Time.now.to_i, Time.now.usec, rand(0xFFFFFFFF)].join(":"))
101
+ s << Digest::SHA1.hexdigest(s)
102
+ end
103
+ end
104
+
105
+ def on_channel_close(channel)
106
+ @state = :closed
107
+ @channel = nil
108
+ end
109
+
110
+ private
111
+
112
+ def open_succeeded(channel)
113
+ @state = :pty
114
+ channel.on_close(&method(:on_channel_close))
115
+ channel.request_pty(:modes => { Net::SSH::Connection::Term::ECHO => 0 }, &method(:pty_requested))
116
+ end
117
+
118
+ def open_failed(channel, code, description)
119
+ @state = :closed
120
+ raise "could not open channel for process manager (#{description}, ##{code})"
121
+ end
122
+
123
+ def pty_requested(channel, success)
124
+ @state = :shell
125
+ raise "could not request pty for process manager" unless success
126
+ if shell == :default
127
+ channel.send_channel_request("shell", &method(:shell_requested))
128
+ else
129
+ channel.exec(shell, &method(:shell_requested))
130
+ end
131
+ end
132
+
133
+ def shell_requested(channel, success)
134
+ @state = :initializing
135
+ raise "could not request shell for process manager" unless success
136
+ channel.on_data(&method(:look_for_initialization_done))
137
+ channel.send_data "export PS1=; echo #{separator} $?\n"
138
+ end
139
+
140
+ def look_for_initialization_done(channel, data)
141
+ if data.include?(separator)
142
+ @state = :open
143
+ @when_open.each { |callback| callback.call(self) }
144
+ @when_open.clear
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ class Net::SSH::Connection::Session
152
+ # Provides a convenient way to initialize a shell given a Net::SSH
153
+ # session. Yields the new shell if a block is given. Returns the shell
154
+ # instance.
155
+ def shell(*args)
156
+ shell = Net::SSH::Shell.new(self, *args)
157
+ yield shell if block_given?
158
+ return shell
159
+ end
160
+ end
@@ -0,0 +1,112 @@
1
+ module Net; module SSH; class Shell
2
+
3
+ class Process
4
+ attr_reader :state
5
+ attr_reader :command
6
+ attr_reader :manager
7
+ attr_reader :callback
8
+ attr_reader :exit_status
9
+ attr_reader :properties
10
+
11
+ def initialize(manager, command, callback)
12
+ @command = command
13
+ @manager = manager
14
+ @callback = callback
15
+ @properties = {}
16
+ @on_output = Proc.new { |p, data| print(data) }
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
+ @master_onclose = manager.channel.on_close(&method(:on_close))
40
+
41
+ cmd = command.dup
42
+ cmd << ";" if cmd !~ /[;&]$/
43
+ cmd << " DONTEVERUSETHIS=$?; echo #{manager.separator} $DONTEVERUSETHIS; echo \"exit $DONTEVERUSETHIS\"|sh"
44
+
45
+ send_data(cmd + "\n")
46
+ callback.call(self) if callback
47
+ end
48
+ end
49
+
50
+ self
51
+ end
52
+
53
+ def starting?
54
+ state == :starting
55
+ end
56
+
57
+ def running?
58
+ state == :running
59
+ end
60
+
61
+ def finished?
62
+ state == :finished
63
+ end
64
+
65
+ def busy?
66
+ starting? || running?
67
+ end
68
+
69
+ def wait!
70
+ manager.session.loop { busy? }
71
+ self
72
+ end
73
+
74
+ def on_output(&callback)
75
+ @on_output = callback
76
+ end
77
+
78
+ def on_finish(&callback)
79
+ @on_finish = callback
80
+ end
81
+
82
+ private
83
+
84
+ def output!(data)
85
+ return unless @on_output
86
+ @on_output.call(self, data)
87
+ end
88
+
89
+ def on_stdout(ch, data)
90
+ if data.strip =~ /^#{manager.separator} (\d+)$/
91
+ before = $`
92
+ output!(before) unless before.empty?
93
+ finished!($1)
94
+ else
95
+ output!(data)
96
+ end
97
+ end
98
+
99
+ def on_close(ch)
100
+ manager.on_channel_close(ch)
101
+ finished!(-1)
102
+ end
103
+
104
+ def finished!(status)
105
+ @state = :finished
106
+ @exit_status = status.to_i
107
+ @on_finish.call(self) if @on_finish
108
+ manager.child_finished(self)
109
+ end
110
+ end
111
+
112
+ end; end; end
@@ -0,0 +1,23 @@
1
+ require 'net/ssh/shell/process'
2
+
3
+ module Net; module SSH; class Shell
4
+
5
+ class Subshell < Process
6
+ private
7
+
8
+ def on_stdout(ch, data)
9
+ if !output!(data)
10
+ ch.on_data(&method(:look_for_finalize_initializer))
11
+ ch.send_data("export PS1=; echo #{manager.separator} $?\n")
12
+ end
13
+ end
14
+
15
+ def look_for_finalize_initializer(ch, data)
16
+ if data =~ /#{manager.separator} (\d+)/
17
+ ch.on_close(&@master_onclose)
18
+ finished!($1)
19
+ end
20
+ end
21
+ end
22
+
23
+ end; end; end
@@ -0,0 +1,22 @@
1
+ require 'net/ssh/version'
2
+
3
+ module Net; module SSH; module Shell
4
+ # A trivial class for representing the version of this library.
5
+ class Version < Net::SSH::Version
6
+ # The major component of the library's version
7
+ MAJOR = 0
8
+
9
+ # The minor component of the library's version
10
+ MINOR = 1
11
+
12
+ # The tiny component of the library's version
13
+ TINY = 0
14
+
15
+ # The library's version as a Version instance
16
+ CURRENT = new(MAJOR, MINOR, TINY)
17
+
18
+ # The library's version as a String instance
19
+ STRING = CURRENT.to_s
20
+ end
21
+ end; end; end
22
+
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{net-ssh-shell}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jamis Buck"]
9
+ s.date = %q{2010-10-25}
10
+ s.description = %q{A simple library to aid with stateful shell interactions}
11
+ s.email = %q{jamis@jamisbuck.org}
12
+ s.extra_rdoc_files = ["CHANGELOG.rdoc", "lib/net/ssh/shell/process.rb", "lib/net/ssh/shell/subshell.rb", "lib/net/ssh/shell/version.rb", "lib/net/ssh/shell.rb", "README.rdoc"]
13
+ s.files = ["CHANGELOG.rdoc", "lib/net/ssh/shell/process.rb", "lib/net/ssh/shell/subshell.rb", "lib/net/ssh/shell/version.rb", "lib/net/ssh/shell.rb", "Rakefile", "README.rdoc", "Manifest", "net-ssh-shell.gemspec"]
14
+ s.homepage = %q{http://net-ssh.rubyforge.org/shell}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Net-ssh-shell", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{net-ssh-shell}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{A simple library to aid with stateful shell interactions}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<net-ssh>, [">= 2.0.9"])
27
+ else
28
+ s.add_dependency(%q<net-ssh>, [">= 2.0.9"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<net-ssh>, [">= 2.0.9"])
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: net-ssh-shell
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jamis Buck
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-25 00:00:00 +04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: net-ssh
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 29
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 9
34
+ version: 2.0.9
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: A simple library to aid with stateful shell interactions
38
+ email: jamis@jamisbuck.org
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - CHANGELOG.rdoc
45
+ - lib/net/ssh/shell/process.rb
46
+ - lib/net/ssh/shell/subshell.rb
47
+ - lib/net/ssh/shell/version.rb
48
+ - lib/net/ssh/shell.rb
49
+ - README.rdoc
50
+ files:
51
+ - CHANGELOG.rdoc
52
+ - lib/net/ssh/shell/process.rb
53
+ - lib/net/ssh/shell/subshell.rb
54
+ - lib/net/ssh/shell/version.rb
55
+ - lib/net/ssh/shell.rb
56
+ - Rakefile
57
+ - README.rdoc
58
+ - Manifest
59
+ - net-ssh-shell.gemspec
60
+ has_rdoc: true
61
+ homepage: http://net-ssh.rubyforge.org/shell
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --line-numbers
67
+ - --inline-source
68
+ - --title
69
+ - Net-ssh-shell
70
+ - --main
71
+ - README.rdoc
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 11
89
+ segments:
90
+ - 1
91
+ - 2
92
+ version: "1.2"
93
+ requirements: []
94
+
95
+ rubyforge_project: net-ssh-shell
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: A simple library to aid with stateful shell interactions
100
+ test_files: []
101
+