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
-
.disable_ecd_sha2_algorithms ⇒ Object
HACK: disable some algorithms.
-
.disable_ed25519_keys ⇒ Object
HACK: disable some key type.
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
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/aspera/ssh.rb', line 37 def initialize(host, username, ) Aspera.assert_type(host, String) Aspera.assert_type(username, String) Aspera.assert_type(, Hash) @host = host @username = username = .dup [:logger] = Log.log unless .key?(:logger) [:verbose] = :warn unless .key?(:verbose) [:use_agent] = false unless .key?(:use_agent) Log.log.debug{"ssh:#{@username}@#{@host}"} Log.dump(:ssh_options, ) end |
Class Method Details
.disable_ecd_sha2_algorithms ⇒ Object
HACK: disable some algorithms
29 30 31 32 33 |
# File 'lib/aspera/ssh.rb', line 29 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
HACK: disable some key type
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/aspera/ssh.rb', line 15 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
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 82 83 84 |
# File 'lib/aspera/ssh.rb', line 52 def execute(cmd, input: nil, exception: false) Aspera.assert_type(cmd, String) Log.log.debug{"cmd=#{cmd}"} response = [] error = [] Net::SSH.start(@host, @username, ) 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 |