Skip to content

Commit

Permalink
Added :pty option to command
Browse files Browse the repository at this point in the history
Supports the following values:

- false or nil: Do not use pty (default)
- true / any other object: Use pty

If the object passed implements on_stdout(channel, data) and/or on_stderr(channel, data), these will be called back once per line of output from the server. This allows the user to programmatically send interactive data to the server, for example passwords, other options.
  • Loading branch information
robd committed Apr 21, 2015
1 parent b4988dc commit 1ceea81
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/sshkit/backends/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def _execute(*args)
Open3.popen3(cmd.to_command) do |stdin, stdout, stderr, wait_thr|
stdout_thread = Thread.new do
while line = stdout.gets do
cmd.on_stdout(line)
cmd.on_stdout(stdin, line)
output << cmd
end
end

stderr_thread = Thread.new do
while line = stderr.gets do
cmd.on_stderr(line)
cmd.on_stderr(stdin, line)
output << cmd
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/sshkit/backends/netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ def _execute(*args)
exit_status = nil
with_ssh do |ssh|
ssh.open_channel do |chan|
chan.request_pty if Netssh.config.pty
chan.request_pty if command.options[:pty]
chan.exec cmd.to_command do |ch, success|
chan.on_data do |ch, data|
cmd.on_stdout(data)
cmd.on_stdout(ch, data)
output << cmd
end
chan.on_extended_data do |ch, type, data|
cmd.on_stderr(data)
cmd.on_stderr(ch, data)
output << cmd
end
chan.on_request("exit-status") do |ch, data|
Expand Down
7 changes: 5 additions & 2 deletions lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ def started=(new_started)
@started = new_started
end

def on_stdout(data)
def on_stdout(channel, data)
@stdout = data
@full_stdout += data
options[:pty].on_stdout(channel, data) if options[:pty].respond_to?(:on_stdout)
end

def on_stderr(data)
def on_stderr(channel, data)
@stderr = data
@full_stderr += data
options[:pty].on_stderr(channel, data) if options[:pty].respond_to?(:on_stderr)
end

def clear_std_out_and_std_err
Expand Down Expand Up @@ -222,6 +224,7 @@ def to_s

def default_options
{
pty: SSHKit::Backend::Netssh.config.pty,
raise_on_non_zero_exit: true,
run_in_background: false
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ def test_failure?

def test_appending_stdout
c = Command.new(:whoami)
c.on_stdout("test\n")
c.on_stdout(nil, "test\n")
assert_equal "test\n", c.stdout
end

def test_appending_stderr
c = Command.new(:whoami)
c.on_stderr("test\n")
c.on_stderr(nil, "test\n")
assert_equal "test\n", c.stderr
end

Expand Down

0 comments on commit 1ceea81

Please sign in to comment.