Ruby,net / ssh与代理连接到路由器,通道挂起

时间:2016-08-24 09:54:15

标签: ruby shell ssh

我目前的网络设置如下: LocalMachine< --- SSH --->服务器< --- Telnet --->路由器。 我试图通过Server从LocalMachine连接到路由器(不能直接连接,服务器充当代理的一种)。 Ruby的net / ssh gem用于与服务器的ssh连接。

require 'net/ssh'


@hostname = "10.16.96.40"
@username = "server_username"
@password = "server_password"

Net::SSH::start(@hostname, @username, :password => @password) do |session|
    session.open_channel do |channel|
        channel.send_channel_request "shell" do |ch, success|
            if success
                puts "user shell started successfully"
            else
                puts "could not start user shell"
            end
        end

        channel.send_data("telnet 192.168.11.201\n")
        channel.send_data("router_username\n")
        channel.send_data("router_password\n")
        channel.send_data("sh ip int brief\n")    

    channel.on_data do |ch, data|
            print data
        end   
    end     
    session.loop
end 

脚本连接到路由器。它得到打印输出。但是当预计会被关闭时,会话就会挂起。

当我使用Ctrl + C手动停止脚本执行时,我收到以下消息:

^C/usr/local/lib/ruby/gems/2.2.0/gems/net-ssh-3.2.0/lib/net/ssh/ruby_compat.rb:25:in `select': Interrupt
    from /usr/local/lib/ruby/gems/2.2.0/gems/net-ssh-3.2.0/lib/net/ssh/ruby_compat.rb:25:in `io_select'
    from /usr/local/lib/ruby/gems/2.2.0/gems/net-ssh-3.2.0/lib/net/ssh/connection/session.rb:210:in `process'
    from /usr/local/lib/ruby/gems/2.2.0/gems/net-ssh-3.2.0/lib/net/ssh/connection/session.rb:170:in `block in loop'
    from /usr/local/lib/ruby/gems/2.2.0/gems/net-ssh-3.2.0/lib/net/ssh/connection/session.rb:170:in `loop'
    from /usr/local/lib/ruby/gems/2.2.0/gems/net-ssh-3.2.0/lib/net/ssh/connection/session.rb:170:in `loop'
    from 25_net_ssh_channel_shell.rb:33:in `block in <main>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/net-ssh-3.2.0/lib/net/ssh.rb:240:in `start'
    from 25_net_ssh_channel_shell.rb:9:in `<main>'

知道如何关闭频道/会话并摆脱闪烁的光标吗?

0 个答案:

没有答案