Sha256: 8905eb914317ec7f990d11008bebdf123de8d6989d43237913ab31134c4d929a

Contents?: true

Size: 1.33 KB

Versions: 15

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.)

{{{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

15 entries across 15 versions & 1 rubygems

Version Path
net-ssh-1.0.10 doc/manual/parts/0023.txt
net-ssh-1.0.1 doc/manual/parts/0023.txt
net-ssh-1.0.3 doc/manual/parts/0023.txt
net-ssh-1.0.4 doc/manual/parts/0023.txt
net-ssh-1.0.7 doc/manual/parts/0023.txt
net-ssh-1.0.5 doc/manual/parts/0023.txt
net-ssh-1.0.9 doc/manual/parts/0023.txt
net-ssh-1.0.6 doc/manual/parts/0023.txt
net-ssh-1.0.8 doc/manual/parts/0023.txt
net-ssh-1.0.2 doc/manual/parts/0023.txt
net-ssh-1.1.1 doc/manual/parts/0023.txt
net-ssh-1.1.0 doc/manual/parts/0023.txt
net-ssh-1.1.2 doc/manual/parts/0023.txt
net-ssh-1.1.3 doc/manual/parts/0023.txt
net-ssh-1.1.4 doc/manual/parts/0023.txt