Class: Aspera::Ssh
- Inherits:
-
Object
- Object
- Aspera::Ssh
- Defined in:
- lib/aspera/ssh.rb
Overview
A simple wrapper around Net::SSH executes one command and get its result from stdout
Defined Under Namespace
Classes: Error
Class Method Summary collapse
Instance Method Summary collapse
-
#execute(cmd, input: nil, exception: false) ⇒ Object
Anything on stderr raises an exception.
-
#initialize(host, username, ssh_options) ⇒ Ssh
constructor
ssh_options: same as Net::SSH.start see: net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start.
Constructor Details
#initialize(host, username, ssh_options) ⇒ Ssh
ssh_options: same as Net::SSH.start see: net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/aspera/ssh.rb', line 35 def initialize(host, username, ) Log.log.debug{"ssh:#{username}@#{host}"} Log.log.debug{"ssh_options:#{}"} Aspera.assert_type(host, String) Aspera.assert_type(username, String) Aspera.assert_type(, Hash) @host = host @username = username @ssh_options = @ssh_options[:logger] = Log.log end |
Class Method Details
.disable_ecd_sha2_algorithms ⇒ Object
27 28 29 30 31 |
# File 'lib/aspera/ssh.rb', line 27 def disable_ecd_sha2_algorithms Log.log.debug('Disabling SSH ecdsa') Net::SSH::Transport::Algorithms::ALGORITHMS.each_value{ |a| a.reject!{ |a| a =~ /^ecd(sa|h)-sha2/}} Net::SSH::KnownHosts::SUPPORTED_TYPE.reject!{ |t| t =~ /^ecd(sa|h)-sha2/} end |
.disable_ed25519_keys ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/aspera/ssh.rb', line 14 def disable_ed25519_keys Log.log.debug('Disabling SSH ed25519 user keys') old_verbose = $VERBOSE $VERBOSE = nil Net::SSH::Authentication::Session.class_eval do define_method(:default_keys) do %w[.ssh .ssh2].product(%w[rsa dsa ecdsa]).map{"~/#{_1}/id_#{_2}"}.freeze end private(:default_keys) end rescue nil $VERBOSE = old_verbose end |
Instance Method Details
#execute(cmd, input: nil, exception: false) ⇒ Object
Anything on stderr raises an exception
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 |
# File 'lib/aspera/ssh.rb', line 48 def execute(cmd, input: nil, exception: false) Aspera.assert_type(cmd, String) Log.log.debug{"cmd=#{cmd}"} response = [] error = [] Net::SSH.start(@host, @username, @ssh_options) do |session| ssh_channel = session.open_channel do |channel| # prepare stdout processing channel.on_data{ |_chan, data| response.push(data)} # prepare stderr processing, stderr if type = 1 channel.on_extended_data do |_chan, _type, data| error.push(data) end channel.on_request('exit-status') do |_ch, data| exit_code = data.read_long next if exit_code.zero? = "#{cmd}: exit #{exit_code}, #{error.join.chomp}" raise Error, if exception # Happens when windows user hasn't logged in and created home account. += "\nHint: home not created in Windows?" if data.include?('Could not chdir to home directory') Log.log.debug() end # send command to SSH channel (execute) cspell: disable-next-line channel.send('cexe'.reverse, cmd){ |_ch, _success| channel.send_data(input) unless input.nil?} end # wait for channel to finish (command exit) ssh_channel.wait # main SSH session loop session.loop end # response as single string return response.join end |