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