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



73
74
75
# File 'lib/audit/lib/connection/ssh_connection.rb', line 73

def <<(command)
	exec(command)
end

#abortObject

force the connection to close



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/audit/lib/connection/ssh_connection.rb', line 241

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



69
70
71
# File 'lib/audit/lib/connection/ssh_connection.rb', line 69

def close
	@ssh_session.close
end

#closed?Boolean

if the connection is closed

Returns:

  • (Boolean)


222
223
224
# File 'lib/audit/lib/connection/ssh_connection.rb', line 222

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

#copy_from_remote(remote_file, local_path) ⇒ Object

copy remote file to local path



175
176
177
178
179
180
181
182
183
184
# File 'lib/audit/lib/connection/ssh_connection.rb', line 175

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



170
171
172
# File 'lib/audit/lib/connection/ssh_connection.rb', line 170

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



196
197
198
# File 'lib/audit/lib/connection/ssh_connection.rb', line 196

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

#exec(command, stdin = nil) ⇒ Object

remote execute a command



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/audit/lib/connection/ssh_connection.rb', line 78

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



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
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/audit/lib/connection/ssh_connection.rb', line 116

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



201
202
203
204
# File 'lib/audit/lib/connection/ssh_connection.rb', line 201

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



217
218
219
# File 'lib/audit/lib/connection/ssh_connection.rb', line 217

def on_close(&block)
	@close_handler = block
end

#on_stderr(&block) ⇒ Object

hook a handler who is called on text on stderr



212
213
214
# File 'lib/audit/lib/connection/ssh_connection.rb', line 212

def on_stderr(&block)
	@stderr_handler = block
end

#on_stdout(&block) ⇒ Object

hook a handler who is called on text on stdout.



207
208
209
# File 'lib/audit/lib/connection/ssh_connection.rb', line 207

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
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

	connected = false
	trials = 5
	while !connected and trials > 0
		begin
			@ssh_session = Net::SSH.start(host, user, parameters)
			connected = true
		rescue Exception => e
			@@logger.warn("connection attempt failed due to #{e.backtrace}")
		end
		trials -= 1
		if !connected
			sleep(20)
		end
	end

	if block then
		yield self
		close
	end
end

#to_hashObject



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

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

#to_sObject

return a string representation of the connection



227
228
229
# File 'lib/audit/lib/connection/ssh_connection.rb', line 227

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



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

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