代码之家  ›  专栏  ›  技术社区  ›  Kimmo Lehto

使用Net::SSH或创建STDIN套接字的交互式SSH会话

  •  2
  • Kimmo Lehto  · 技术社区  · 7 年前

    How to SSH interactive Session Net::SSH interactive terminal scripting. How? Ruby net-ssh interactive response/reply

    我可以向你倾诉 system "ssh" ssh 参数。

    STDIN 去贝壳频道。的Net::SSH文档 listen_to 演示如何在输入来自套接字而不是STDIN时执行此操作。 $stdin IO.console Net::SSH::BufferedIo .

    有没有办法从中创建套接字 标准 可以用来做这个吗?或者有没有更好的方法把所有的东西从 标准 Net::SSH::Channel

    下面的代码可以正常工作,但速度太慢,无法使用:

    require 'net/ssh'
    require 'io/console'
    require 'io/wait'
    
    Net::SSH.start('localhost', 'root') do |session|
      session.open_channel do |channel|
        channel.on_data do |_, data|
          $stdout.write(data)
        end
    
        channel.on_extended_data do |_, data|
          $stdout.write(data)
        end
    
        channel.request_pty do
          channel.send_channel_request "shell"
        end
    
        channel.connection.loop do
          $stdin.raw do |io|
            input = io.readpartial(1024)
            channel.send_data(input) unless input.empty?
          end
          channel.active?
        end
      end.wait
    end
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Casper    7 年前

    套接字实际上只不过是文件描述符,由于STDIN也是一个文件描述符,所以尝试一下也无妨。


    此代码似乎工作正常:

    begin
      system('stty cbreak -echo')
    
      Net::SSH.start(...) do |session|
        session.open_channel do |...|
           ...
           session.listen_to(STDIN) { |stdin|
             input = stdin.readpartial(1024)
             channel.send_data(input) unless input.empty?
           }
         end.wait
       end
    ensure
      system('stty sane')
    end
    
    推荐文章