Sha256: 6b1ae86cf7e5921b6253658e8fc552c0b3e7af7e5336a596024f9844e47e612b

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true
require 'net/ssh'
require 'net/scp'

class SSHClient
  attr_reader :server, :client

  def initialize(server)
    @server = server
    @client = nil
  end

  def method_missing(command, *args)
    command_string = command.to_s
    if run("command -v #{command_string}")
      puts "Executing command: #{command} #{args.join(' ')}"
      output = run("#{command} #{args.join(' ')}")
      puts output
    else
      super
    end
  end

  def connect
    @client = Net::SSH.start(@server.host, @server.user, password: @server.password)
  rescue StandardError => e
    puts "Error connecting to #{@server.host}: #{e.message}"
  end

  def run1(command)
    return unless @client

    @client.exec!(command)
  rescue StandardError => e
    puts "Error executing command on #{@host}: #{e.message}"
    nil
  end

  def run(command)
    connect unless @client
    output = ""
    @client.exec!(command) do |channel, stream, data|
      output = data
    end
    output
  end

  def put(local_file, remote_path)
    connect unless @client
    Net::SCP.upload!(@client.connection, local_file, remote_path)
  end

  def close
    @client.close if @client
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wiz-teleport-0.5.1 lib/wiz-teleport/ssh_client.rb
wiz-teleport-0.5.0 lib/wiz-teleport/ssh_client.rb
wiz-teleport-0.4.0 lib/wiz-teleport/ssh_client.rb