2011-04-13 69 views
13

我在目錄usr/share/ruby.rb中有一個文件。我想通過使用Ruby調用使用SSH和SCP將該文件傳輸到基於IP的遠程設備。誰能幫我?如何使用Ruby調用使用SSH和SCP傳輸文件?

+2

重新問同樣的問題是不能接受的行爲在這裏。如果你想引起人們對你的問題的關注,那麼兩天後你將被允許[放置賞金](http://stackoverflow.com/faq#bounty)。您也可以[編輯您的問題](http://stackoverflow.com/posts/5644110/edit)添加其他信息,這可能會使您的問題更易於理解和回答。 – Will 2011-04-14 18:09:35

回答

12

Net::SSH圖書館包括Net::SCP,所以你應該開始在那裏看。

從網:: SCP文檔:

 
    require 'net/scp' 

    # upload a file to a remote server 
    Net::SCP.upload!("remote.host.com", "username", 
    "/local/path", "/remote/path", 
    :password => "password") 

    # download a file from a remote server 
    Net::SCP.download!("remote.host.com", "username", 
    "/remote/path", "/local/path", 
    :password => password) 

    # download a file to an in-memory buffer 
    data = Net::SCP::download!("remote.host.com", "username", "/remote/path") 
+0

非常感謝..我會檢查它是否有效.. – user705217 2011-04-13 12:38:37

+0

您需要修改樣本以適合您的情況。示例代碼不會讓您吃魚,它會向您展示用於捕捉自己的魚的適當的東西。 – 2011-04-14 18:08:19

+0

嗨田,當我運行腳本,它顯示NoMethod錯誤..這是什麼意思??這是我的腳本..需要淨/ scp – user705217 2011-04-17 07:32:12

20

例如:

require 'net/scp' 

    host = '10.10.10.10' 
    login = 'foo' 
    password = 'bar' 

    Net::SCP.start(host, login, :password => password) do |scp| 
     puts 'SCP Started!' 
     scp.download('/usr/share/ruby.rb', '.') 
    end 

存在也是一個scp.upload