【问题标题】:ruby telnet to windows 2008 ,execute command errorruby telnet到windows 2008,执行命令错误
【发布时间】:2013-04-30 21:43:33
【问题描述】:

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

如果执行

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

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

例外:

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?

Buf 如果执行

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

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

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

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

需要你的帮助。

【问题讨论】:

    标签: ruby telnet


    【解决方案1】:

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

    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
    

    【讨论】:

    • 如果您在这个问题上不需要进一步的帮助,请选择一个正确的答案。;
    猜你喜欢
    • 2015-04-03
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 2015-10-30
    • 2012-05-06
    相关资源
    最近更新 更多