ruby telnet到windows 2008,执行命令错误
我尝试使用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")
成功但第二个抛出exception。然后后续命令全部失败。 经过实验,我发现任何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 `'
使用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")
它没有正常运行。 ls
, uname
是安装在目标机器上的厨师带来的一些linux命令。
ruby版本:ruby 1.9.3p286(2012-10-12)[i386-mingw32]
我发现其他人在Stackoverflow上问了同样的问题,但他没有得到解决方案。 http://www.ruby-forum.com/topic/1516840
需要你的帮助。
解决了。 原因是ruby net / telnet库使用错误换行分隔符。 必须是EOL(CR + LF)但CR + NULL。 但我不知道是谁制造了虫子,窗户还是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