ruby telnet 到 windows 2008,执行命令出错 [英] ruby telnet to windows 2008 ,execute command error

查看:28
本文介绍了ruby telnet 到 windows 2008,执行命令出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 ruby​​ Net::Telnet 连接 windows 2008 并执行一些命令.但是失败了.

I tried to use ruby Net::Telnet to connect windows 2008 and execute some commands. But it failed.

如果执行

tn = Net::Telnet::new("Host"=>"walnutserver","Port"=>2300,"Prompt"=> /C:.*>/)
tn.login("user","pass")
tn.cmd("dir")
tn.cmd("dir")

第一个 tn.cmd("dir") 成功,但第二个抛出异常.然后后续命令都失败了.经过实验,我发现任何windows命令都会导致这个.

the first tn.cmd("dir") is success but the second one throws exceptions.And then subsequent commands all failed. After experimenting,I found that any windows command will cause this.

例外:

Timeout::Error: timed out while waiting for more data
        from c:/troy/data/chef/chef-client11/chef/embedded/lib/ruby/1.9.1/net/telnet.rb:558:in `waitfor'
        from c:/troy/data/chef/chef-client11/chef/embedded/lib/ruby/1.9.1/net/telnet.rb:697:in `cmd'
        from (irb):20
        from c:/troy/data/chef/chef-client11/chef/embedded/bin/irb:12:in `<main>'

使用sock.sysread()方法读取响应,发现终端被阻塞并显示dir\r\n0x00More?

use sock.sysread() method to read responding, I found that terminal is blocked and display dir\r\n0x00More?

如果执行则缓冲

tn = Net::Telnet::new("Host"=>"walnutserver","Port"=>2300,"Prompt"=> /C:.*>/)
tn.login("user","pass")
tn.cmd("ls")
tn.cmd("uname")

它没有正常运行.lsuname是chef带来的一些linux命令,安装在目标机器上.

It't running normally. lsuname are some linux commands brought by chef which installed in target machine.

ruby 版本:ruby 1.9.3p286 (2012-10-12) [i386-mingw32]

ruby version:ruby 1.9.3p286 (2012-10-12) [i386-mingw32]

我找到了别人在 Stackoverflow 上问同样的问题,但他没有得到解决方案.http://www.ruby-forum.com/topic/1516840

I found someone else asking the same question on Stackoverflow, but he didn't get the solution. http://www.ruby-forum.com/topic/1516840

需要你的帮助.

推荐答案

已解决.原因是 ruby​​ net/telnet 库使用错误换行符.必须是 EOL(CR+LF) 但 CR+NULL .但我不知道是谁制造了错误,windows 还是 ruby​​?我写了一个猴子补丁如下:

solved. the reason is ruby net/telnet library use error newline seperator. Must be EOL(CR+LF) but CR+NULL . But I don't know who make the bug,windows or ruby? I write a monkey patch as below:

class Net::Telnet
    def print(string)
      string = string.gsub(/#{IAC}/no, IAC + IAC) if @options["Telnetmode"]

      if @options["Binmode"]
        self.write(string)
      else
        if @telnet_option["BINARY"] and @telnet_option["SGA"]
          self.write(string.gsub(/\n/n, CR))
        elsif @telnet_option["SGA"]
          self.write(string.gsub(/\n/n, EOL)) ### fix here. reaplce CR+NULL bY EOL
        else
          self.write(string.gsub(/\n/n, EOL))
        end
      end
    end
end

这篇关于ruby telnet 到 windows 2008,执行命令出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆