Sha256: 4d0b13f39a78b56478dab2d2673f4d41438c9df986a528ca396c9ce3ea881a62

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

Using the shell service and pty's, you can now create a simple SSH terminal client. (You'll also want to download and install the "ruby-termios":http://arika.org/ruby/termios library so that your input is not interpreted in a linewise fashion.)

[!figure lang=ruby,number=true,caption=A simple SSH terminal client in Ruby
begin
  require 'termios'
rescue LoadError
end

def stdin_buffer( enable )
  return unless defined?( Termios )
  attr = Termios::getattr( $stdin )
  if enable
    attr.c_lflag |= Termios::ICANON | Termios::ECHO
  else
    attr.c_lflag &= ~(Termios::ICANON|Termios::ECHO)
  end
  Termios::setattr( $stdin, Termios::TCSANOW, attr )
end

host = ARGV.shift or abort "You must specify the [user@]host to connect to"
if host =~ /@/
  user, host = host.match( /(.*?)@(.*)/ )[1,2]
else
  user = ENV['USER'] || ENV['USER_NAME']
end

Net::SSH.start( host, user ) do |session|

  begin
    stdin_buffer false

    shell = session.shell.open( :pty => true )

    loop do
      break unless shell.open?
      if IO.select([$stdin],nil,nil,0.01)
        data = $stdin.sysread(1)
        shell.send_data data
      end

      $stdout.print shell.stdout while shell.stdout?
      $stdout.flush
    end
  ensure
    stdin_buffer true
  end

end
!]

The above code is also available as an example script in the Net::SSH distribution (@examples/ssh-client.rb@).

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
net-ssh-0.9.0 doc/manual/parts/shells_clients.txt
net-ssh-1.0.0 doc/manual/parts/shells_clients.txt