Class: Channelizer::Channels::Ssh
- Defined in:
- lib/channelizer/channels/ssh.rb
Instance Method Summary collapse
-
#execute(command, options = { }) ⇒ Fixnum
Executes a command on the channel.
-
#initialize(options) ⇒ Ssh
constructor
Creates a SSH Session.
- #session ⇒ Object
-
#sudo_command(command) ⇒ String
Echos the command (Windows does not support sudo).
-
#upload(source, destination, options = {}) ⇒ Fixnum
Uploads a file over WinRM.
Methods inherited from Base
#ready?, required_option, #shell_execute, validate_options
Methods included from Util::Retryable
Constructor Details
#initialize(options) ⇒ Ssh
Creates a SSH Session
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/channelizer/channels/ssh.rb', line 18 def initialize() defaults = { :port => 22, :paranoid => false } = defaults.merge() # Options need to be in a final state before we call the parent initializer super() @connection_options = end |
Instance Method Details
#execute(command, options = { }) ⇒ Fixnum
Executes a command on the channel
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/channelizer/channels/ssh.rb', line 41 def execute(command, = { }) defaults = { :out_console => true } = defaults.merge() # open a new channel and configure a minimal set of callbacks, then run # the event loop until the channel finishes (closes) last_exit = -1 channel = session.open_channel do |ch| #request pty for sudo stuff and so ch.request_pty do |ch, success| raise "Error requesting pty" unless success end ch.exec "#{command}" do |ch, success| raise "could not execute command" unless success # "on_data" is called when the process writes something to stdout ch.on_data do |c, data| STDOUT.print data if [:out_console] end # "on_extended_data" is called when the process writes something to stderr ch.on_extended_data do |c, type, data| STDOUT.print data if [:out_console] end channel.on_request("exit-signal") do |ch, data| last_exit = data.read_long end channel.on_request("exit-status") do |ch,data| last_exit = data.read_long end end end channel.wait last_exit end |
#session ⇒ Object
29 30 31 32 33 34 |
# File 'lib/channelizer/channels/ssh.rb', line 29 def session = @connection_options.clone host = .delete(:host) username = .delete(:username) Net::SSH.start(host,username,) end |
#sudo_command(command) ⇒ String
Echos the command (Windows does not support sudo)
87 88 89 |
# File 'lib/channelizer/channels/ssh.rb', line 87 def sudo_command(command) "sudo #{command}" end |
#upload(source, destination, options = {}) ⇒ Fixnum
Uploads a file over WinRM
96 97 98 99 100 101 102 |
# File 'lib/channelizer/channels/ssh.rb', line 96 def upload(source, destination, = {} ) = @connection_options.clone host = .delete(:host) username = .delete(:username) channel = session.scp.upload(source,destination) channel.wait end |