![](/img/trans.png)
[英]Ruby and Net::SSH Interfacing with a interactive command line application
[英]ssh and rsync in one command line from Ruby
我需要編寫一個使用ssh連接到“ first_machine”的bash命令行,然后從此處運行rsync
到另一台計算機remoteuser@10.2.2.150
。
這是我的代碼:
first_machine = user@192.16.58.12
cmd="ssh #{first_machine} \"rsync -e\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" --archive -H /home/user/aaaaa remoteuser@10.2.2.150:/home/remoteuser/\" "
exit_status = system("#{cmd}")
我分別測試了ssh命令和rsync
。 它們工作正常,因此我可以在運行Ruby腳本的計算機上執行ssh並執行以下操作:
rsync -e"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --archive -H /home/user/aaaaa remoteuser@10.2.2.150:/home/remoteuser/
在#{first_machine}
->文件夾中啟動后,復制效果很好。
相反,當我通過Ruby腳本運行代碼時,收到錯誤消息:
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(605) [sender=3.0.9]
如果我單獨運行命令,則不會發生此錯誤。
當“單獨運行命令”時,將以具有已知權限和已設置環境的用戶身份運行它。
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
提示您從代碼登錄時,該帳戶的權限不正確。 這不是Ruby問題,這是由於未使用正確的帳戶或未設置環境導致的。 在露比外面。
首先,解決其他答案中“錫人”指出的用戶問題。 在first_machine
和運行ruby
腳本的用戶之間first_machine
設置了公鑰身份驗證?
然后,查看您的引用,您將看到傳遞給ssh
的命令僅包含rsync -e
。 也許試試這個:
first_machine = 'user@192.16.58.12'
cmd="ssh #{first_machine} \"rsync -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --archive -H /home/user/aaaaa remoteuser@10.2.2.150:/home/remoteuser/\" "
exit_status = system(cmd)
免責聲明:我以前從未使用過Ruby,但是一眼瀏覽一些教程站點就可以清楚地看到示例代碼中有語法錯誤。
有沒有理由不作為bash腳本執行此操作?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.