lib/net/ssh/session.rb in net-ssh-session-0.1.0 vs lib/net/ssh/session.rb in net-ssh-session-0.1.1
- old
+ new
@@ -14,22 +14,28 @@
attr_reader :options
attr_reader :logger
attr_reader :history
# Initialize a new ssh session
- # @param host [String] remote hostname or ip address
- # @param user [String] remote account username
- # @param password [String] remote account password
- def initialize(host, user, password='')
- @host = host
- @user = user
- @password = password
- @history = []
+ # @param [String] remote hostname or ip address
+ # @param [String] remote account username
+ # @param [String] remote account password
+ # @param [Hash] options hash
+ def initialize(host, user, password='', options={})
+ @host = host
+ @user = user
+ @password = password
+ @history = []
+ @track_history = true
+
+ if options[:history] == false
+ @track_history = false
+ end
end
# Establish connection with remote server
- # @param timeout [Integer] max timeout in seconds
+ # @param [Integer] max timeout in seconds
# @return [Boolean]
def open(timeout=nil)
if timeout && timeout > 0
with_timeout(timeout) { establish_connection }
else
@@ -46,12 +52,12 @@
def on_output(&block)
@on_output = block
end
# Execute command
- # @param command [String] command to execute
- # @param on_output [Block] output event block
+ # @param [String] command to execute
+ # @param [Block] output event block
# @return [Integer] command execution exit code
def exec(command, &on_output)
status = nil
shell.execute(command) do |process|
process.on_output(&on_output)
@@ -61,12 +67,12 @@
shell.session.loop(1) { status.nil? }
status
end
# Execute a single command
- # @param command [String] comand to execute
- # @param options [Hash] execution options
+ # @param [String] comand to execute
+ # @param [Hash] execution options
# @return [SessionCommand]
def run(command, options={})
output = ''
t_start = Time.now
@@ -80,19 +86,22 @@
cmd = SessionCommand.new(
command, output, exit_code,
t_end - t_start
)
- history << cmd unless options[:history] == false
+ if options[:history] == true || @track_history == true
+ history << cmd
+ end
+
logger.info(cmd.to_s) if logger
cmd
end
# Execute multiple commands
- # @param commands [Array] set of commands to execute
- # @param options [Hash] execution options
+ # @param [Array] set of commands to execute
+ # @param [Hash] execution options
# @return [Array] set of command execution results
#
# Execution options are the following:
# options[:break] - If set to `true`, execution chain will break on first failed command
#
@@ -108,11 +117,18 @@
results
end
# Set a global session logger for commands
+ # @param [Logger] logger instance
def logger=(log)
@logger = log
+ end
+
+ # Get last executed command
+ # @return [SessionCommand]
+ def last_command
+ history.last
end
private
def establish_connection
\ No newline at end of file