Class Net::SSH::Proxy::Command

  1. lib/net/ssh/proxy/command.rb
Parent: Object

An implementation of a command proxy. To use it, instantiate it, then pass the instantiated object via the :proxy key to Net::SSH.start:

require 'net/ssh/proxy/command'

proxy = Net::SSH::Proxy::Command.new('ssh relay nc %h %p')
Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
  ...
end

Methods

public class

  1. new

public instance

  1. open

Attributes

command_line [R] The command line for the session
command_line_template [R] The command line template

Public class methods

new (command_line_template)

Create a new socket factory that tunnels via a command executed with the user’s shell, which is composed from the given command template. In the command template, `%h’ will be substituted by the host name to connect and `%p’ by the port.

[show source]
    # File lib/net/ssh/proxy/command.rb, line 29
29:     def initialize(command_line_template)
30:       @command_line_template = command_line_template
31:       @command_line = nil
32:     end

Public instance methods

open (host, port)

Return a new socket connected to the given host and port via the proxy that was requested when the socket factory was instantiated.

[show source]
    # File lib/net/ssh/proxy/command.rb, line 36
36:     def open(host, port)
37:       command_line = @command_line_template.gsub(/%(.)/) {
38:         case $1
39:         when 'h'
40:           host
41:         when 'p'
42:           port.to_s
43:         when '%'
44:           '%'
45:         else
46:           raise ArgumentError, "unknown key: #{$1}"
47:         end
48:       }
49:       begin
50:         io = IO.popen(command_line, "r+")
51:         if result = Net::SSH::Compat.io_select([io], nil, [io], 60)
52:           if result.last.any?
53:             raise "command failed"
54:           end
55:         else
56:           raise "command timed out"
57:         end
58:       rescue => e
59:         raise ConnectError, "#{e}: #{command_line}"
60:       end
61:       @command_line = command_line
62:       class << io
63:         def send(data, flag)
64:           write_nonblock(data)
65:         end
66: 
67:         def recv(size)
68:           read_nonblock(size)
69:         end
70:       end
71:       io
72:     end