Class: SshConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/audit/lib/connection/ssh_connection.rb

Direct Known Subclasses

AmiConnection

Constant Summary collapse

@@logger =
Logger.new(STDOUT)

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ SshConnection

Returns a new instance of SshConnection.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/audit/lib/connection/ssh_connection.rb', line 9

def initialize(parameters)
	raise "Need parameter :connection_params" unless parameters[:connection_params]
	
	if parameters[:logger] then
		@@logger = parameters[:logger]
	end

	@parameters = parameters[:connection_params]
	@parameters[:user] = 'root' unless @parameters[:user]
	raise "No target host specified" unless @parameters[:host]
end

Instance Method Details

#<<(command) ⇒ Object



60
61
62
# File 'lib/audit/lib/connection/ssh_connection.rb', line 60

def <<(command)
	exec(command)
end

#abortObject

force the connection to close



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/audit/lib/connection/ssh_connection.rb', line 228

def abort()
  if @ssh_session && !@ssh_session.closed? then
    #try to close connection gracefully
    @ssh_session.close
    # and if it won't, send out the hunter to bring in Snowwhite's heart after some time
    Thread.new do
      sleep 5
      @ssh_session.shutdown! unless @ssh_session.nil? || @ssh_session.closed?
    end
  end
end

#closeObject



56
57
58
# File 'lib/audit/lib/connection/ssh_connection.rb', line 56

def close
	@ssh_session.close
end

#closed?Boolean

if the connection is closed

Returns:

  • (Boolean)


209
210
211
# File 'lib/audit/lib/connection/ssh_connection.rb', line 209

def closed?
	return @ssh_session.nil? || @ssh_session.closed?
end

#copy_from_remote(remote_file, local_path) ⇒ Object

copy remote file to local path



162
163
164
165
166
167
168
169
170
171
# File 'lib/audit/lib/connection/ssh_connection.rb', line 162

def copy_from_remote(remote_file, local_path)
	begin
		@@logger.info { "Copying file from #{remote_file} to #{local_path}" }
		@ssh_session.scp.download!(remote_file, local_path)
		return true
	rescue => err
		@@logger.error { "SCP failed: #{err.message}" }
		@@logger.error { "\t #{err.backtrace}" }
	end
end

#copy_to_remote(local_file, remote_path) ⇒ Object

copy local file to remote path



157
158
159
# File 'lib/audit/lib/connection/ssh_connection.rb', line 157

def copy_to_remote(local_file, remote_path)
	@ssh_session.scp.upload! local_file, remote_path
end

#delete_remote(remote_file) ⇒ Object

delete remote file



183
184
185
# File 'lib/audit/lib/connection/ssh_connection.rb', line 183

def delete_remote(remote_file)
	exec("rm #{remote_file}")
end

#exec(command, stdin = nil) ⇒ Object

remote execute a command



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/audit/lib/connection/ssh_connection.rb', line 65

def exec(command, stdin = nil)
  exit_status = 0 # define variable so that it will be available in the block at method scope
  @ssh_session.open_channel do |ch|
    @@logger.info("Executing command '#{command}'")
    ch.exec(command) do|ch, success|

             if success then
        @@logger.debug("Command sucessfully executed")
               ch.on_data() do|ch, data|
                 #process_stdout(data) unless data
                 @stdout_handler.call(data) unless @stdout_handler.nil? or data.nil?
               end
               ch.on_extended_data() do|ch, type, data|
                 @stderr_handler.call(data) unless @stderr_handler.nil? or data.nil?
               end
               ch.on_request "exit-status" do|ch, data|
                 exit_status = data.read_long unless data.nil?
               end
               ch.on_close do |ch|
                 @close_handler.call() unless @close_handler.nil?
               end
               ch.on_eof do |ch|
                 @close_handler.call() unless @close_handler.nil?
               end

               ch.send_data stdin if stdin
             else
        @@logger.debug("")
               exit_status = 127
             end
           end
    ch.wait
  end

  return exit_status
end

#exec_new(command, stdin = nil) ⇒ Object

XXX: new remote execute a command



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/audit/lib/connection/ssh_connection.rb', line 103

def exec_new(command, stdin = nil)
  exit_status = 0 # define variable so that it will be available in the block at method scope
  channel = @ssh_session.open_channel do |ch|
    ch.exec(command) do|ch, success|

           if success then
        @@logger.info("SshConnection: starts executing '#{command}'")
               ch.on_data() do|ch, data|
                 #process_stdout(data) unless data
                 @stdout_handler.call(data) unless @stdout_handler.nil? or data.nil?
               end
               ch.on_extended_data() do|ch, type, data|
                 @stderr_handler.call(data) unless @stderr_handler.nil? or data.nil?
               end
               ch.on_request "exit-status" do|ch, data|
                 exit_status = data.read_long unless data.nil?
          @@logger.info("SshConnection.on_request: process terminated with exit-status: #{exit_status}")
          if exit_status != 0
            @@logger.error("SshConnection.on_request: Remote command execution failed with code: #{exit_status}")
          end
               end
        ch.on_request "exit-signal" do |ch, data|
          @@logger.info("SshConnection.on_request: process terminated with exit-signal: #{data.read_string}")
        end
               ch.on_close do |ch|
          @@logger.info("SshConnection.on_close: remote end is closing!")
                 #@close_handler.call() unless @close_handler.nil?
               end
               ch.on_eof do |ch|
          @@logger.info("SshConnection.on_eof: remote end is done sending data")
                 #@close_handler.call() unless @close_handler.nil?
               end
        ch.on_open_failed do |ch, code, desc|
          @@logger.info("SshConnection.on_open_failed: code=#{code} desc=#{desc}")
        end
        ch.on_process do |ch|
          #@@logger.debug("SshConnection.on_process; send line-feed/sleep")
          ch.send_data("\n")
        end

               #ch.send_data stdin if stdin
           else
        @@logger.debug("SshConnection: the remote command could not be invoked!")
               exit_status = 127
             end
           end
    #ch.wait
  end
  channel.wait
  return exit_status
end

#move_from_remote(remote_file, local_path) ⇒ Object

copies remote file to local path and deletes remote file afterwards



188
189
190
191
# File 'lib/audit/lib/connection/ssh_connection.rb', line 188

def move_from_remote(remote_file, local_path)
	copy_from_remote(remote_file, local_path)
	delete_remote(remote_file)
end

#on_close(&block) ⇒ Object

hook a handler who is called on connection close



204
205
206
# File 'lib/audit/lib/connection/ssh_connection.rb', line 204

def on_close(&block)
	@close_handler = block
end

#on_stderr(&block) ⇒ Object

hook a handler who is called on text on stderr



199
200
201
# File 'lib/audit/lib/connection/ssh_connection.rb', line 199

def on_stderr(&block)
	@stderr_handler = block
end

#on_stdout(&block) ⇒ Object

hook a handler who is called on text on stdout.



194
195
196
# File 'lib/audit/lib/connection/ssh_connection.rb', line 194

def on_stdout(&block)
	@stdout_handler = block
end

#open(&block) ⇒ Object

open the ssh connection to the remote host



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/audit/lib/connection/ssh_connection.rb', line 22

def open(&block)
	if @ssh_session && !@ssh_session.closed? then
		@@logger.warn("trying to open an already opened ssh connection")
		raise "trying to open an already opened ssh connection"
	end
	
	@@logger.info("opening ssh connection with parameters: " + @parameters.to_s)

	parameters = @parameters.clone()
	host = @parameters[:host]
	user = @parameters[:user]
	
	parameters.delete(:host)
	parameters.delete(:user)
	
	if @parameters[:keys] then
		@@logger.info("Starting SSH session with public key authentication")
	elsif @parameters[:key_data] then
		@@logger.info("Starting SSH session with public key authentication")
	elsif @parameters[:password] then
		@@logger.info("Starting SSH session with password authentication")
	else
		@@logger.error("No SSH authentication method found in parameters")
		raise "No authentication method found"
	end
	
	@ssh_session = Net::SSH.start(host, user, parameters)

	if block then
		yield self
		close
	end
end

#to_hashObject



218
219
220
221
222
223
224
225
# File 'lib/audit/lib/connection/ssh_connection.rb', line 218

def to_hash()
  return {
    :type => :CONNECTION,
    :subtype => :ssh,
    :user => @parameters[:user],
    :host => @parameters[:host]
  }
end

#to_sObject

return a string representation of the connection



214
215
216
# File 'lib/audit/lib/connection/ssh_connection.rb', line 214

def to_s()
  return "ssh:#{@parameters[:user]}@#{@parameters[:host]}"
end

#write_to_remote_file(string, remote_path) ⇒ Object

write a string to a remote file



174
175
176
177
178
179
180
# File 'lib/audit/lib/connection/ssh_connection.rb', line 174

def write_to_remote_file(string, remote_path)
  #filename = "/tmp/" + Random.srand().to_s() + ".sh"
  filename = "/tmp/" + Kernel::srand().to_s() + ".sh"
  File.open(filename, 'w') {|f| f.write(string)}
  copy_to_remote(filename, remote_path)
  File.delete(filename)
end