Class: SSHClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wiz-teleport/ssh_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ SSHClient

Returns a new instance of SSHClient.



8
9
10
11
# File 'lib/wiz-teleport/ssh_client.rb', line 8

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/wiz-teleport/ssh_client.rb', line 13

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/wiz-teleport/ssh_client.rb', line 6

def client
  @client
end

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/wiz-teleport/ssh_client.rb', line 6

def server
  @server
end

Instance Method Details

#closeObject



53
54
55
# File 'lib/wiz-teleport/ssh_client.rb', line 53

def close
  @client.close if @client
end

#connectObject



24
25
26
27
28
# File 'lib/wiz-teleport/ssh_client.rb', line 24

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

#put(local_file, remote_path) ⇒ Object



48
49
50
51
# File 'lib/wiz-teleport/ssh_client.rb', line 48

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

#run(command) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/wiz-teleport/ssh_client.rb', line 39

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

#run1(command) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/wiz-teleport/ssh_client.rb', line 30

def run1(command)
  return unless @client

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