2011-07-26 39 views
11

Tôi gặp sự cố khi kết nối qua ssh từ ruby ​​bằng cách sử dụng 'net-ssh' gem, nhận Net :: SSH :: AuthenticationFailed. Đoạn mã dướiKhông thể kết nối bằng cách sử dụng các phím với Ruby và net/ssh

require 'net/ssh' 
keys = ["path_to_private_key"] 
Net::SSH.start('host', 'user',:keys => keys, :verbose => :debug) do |ssh| 
    #ssh code 

end 

Sử dụng ssh trực tiếp từ dòng lệnh hoạt động:

ssh -i <path_to_private_key> [email protected] 

Tôi có ssh API sai? Tôi đã thử cả 'người dùng @ host' và chỉ 'người dùng' làm tên người dùng với cùng một kết quả.

Đây là kết quả gỡ lỗi:

D, [2011-07-26T19:42:00.135148 #3511] DEBUG -- net.ssh.transport.session[140b040]: establishing connection to host:22 
D, [2011-07-26T19:42:00.164190 #3511] DEBUG -- net.ssh.transport.session[140b040]: connection established 
I, [2011-07-26T19:42:00.164344 #3511] INFO -- net.ssh.transport.server_version[140a3fc]: negotiating protocol version 
D, [2011-07-26T19:42:00.192092 #3511] DEBUG -- net.ssh.transport.server_version[140a3fc]: remote is `SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu3' 
D, [2011-07-26T19:42:00.192177 #3511] DEBUG -- net.ssh.transport.server_version[140a3fc]: local is `SSH-2.0-Ruby/Net::SSH_2.1.4 x86_64-linux' 
D, [2011-07-26T19:42:00.212348 #3511] DEBUG -- tcpsocket[140aaf0]: read 784 bytes 
D, [2011-07-26T19:42:00.212461 #3511] DEBUG -- tcpsocket[140aaf0]: received packet nr 0 type 20 len 780 
I, [2011-07-26T19:42:00.212515 #3511] INFO -- net.ssh.transport.algorithms[13e9058]: got KEXINIT from server 
I, [2011-07-26T19:42:00.212604 #3511] INFO -- net.ssh.transport.algorithms[13e9058]: sending KEXINIT 
D, [2011-07-26T19:42:00.212698 #3511] DEBUG -- tcpsocket[140aaf0]: queueing packet nr 0 type 20 len 556 
D, [2011-07-26T19:42:00.212741 #3511] DEBUG -- tcpsocket[140aaf0]: sent 560 bytes 
I, [2011-07-26T19:42:00.212771 #3511] INFO -- net.ssh.transport.algorithms[13e9058]: negotiating algorithms 
D, [2011-07-26T19:42:00.212859 #3511] DEBUG -- net.ssh.transport.algorithms[13e9058]: negotiated: 
* kex: diffie-hellman-group-exchange-sha1 
* host_key: ssh-rsa 
* encryption_server: aes128-cbc 
* encryption_client: aes128-cbc 
* hmac_client: hmac-sha1 
* hmac_server: hmac-sha1 
* compression_client: none 
* compression_server: none 
* language_client: 
* language_server: 
D, [2011-07-26T19:42:00.212889 #3511] DEBUG -- net.ssh.transport.algorithms[13e9058]: exchanging keys 
D, [2011-07-26T19:42:00.212998 #3511] DEBUG -- tcpsocket[140aaf0]: queueing packet nr 1 type 34 len 20 
D, [2011-07-26T19:42:00.213033 #3511] DEBUG -- tcpsocket[140aaf0]: sent 24 bytes 
D, [2011-07-26T19:42:00.292238 #3511] DEBUG -- tcpsocket[140aaf0]: read 152 bytes 
D, [2011-07-26T19:42:00.292389 #3511] DEBUG -- tcpsocket[140aaf0]: received packet nr 1 type 31 len 148 
D, [2011-07-26T19:42:00.297526 #3511] DEBUG -- tcpsocket[140aaf0]: queueing packet nr 2 type 32 len 140 
D, [2011-07-26T19:42:00.297627 #3511] DEBUG -- tcpsocket[140aaf0]: sent 144 bytes 
D, [2011-07-26T19:42:00.321754 #3511] DEBUG -- tcpsocket[140aaf0]: read 720 bytes 
D, [2011-07-26T19:42:00.321909 #3511] DEBUG -- tcpsocket[140aaf0]: received packet nr 2 type 33 len 700 
D, [2011-07-26T19:42:00.326688 #3511] DEBUG -- tcpsocket[140aaf0]: queueing packet nr 3 type 21 len 20 
D, [2011-07-26T19:42:00.326793 #3511] DEBUG -- tcpsocket[140aaf0]: sent 24 bytes 
D, [2011-07-26T19:42:00.326927 #3511] DEBUG -- tcpsocket[140aaf0]: received packet nr 3 type 21 len 12 
D, [2011-07-26T19:42:00.327325 #3511] DEBUG -- net.ssh.authentication.session[1475c10]: beginning authentication of `[email protected]' 
D, [2011-07-26T19:42:00.327519 #3511] DEBUG -- tcpsocket[140aaf0]: queueing packet nr 4 type 5 len 28 
D, [2011-07-26T19:42:00.327604 #3511] DEBUG -- tcpsocket[140aaf0]: sent 52 bytes 
D, [2011-07-26T19:42:00.461124 #3511] DEBUG -- tcpsocket[140aaf0]: read 52 bytes 
D, [2011-07-26T19:42:00.461199 #3511] DEBUG -- tcpsocket[140aaf0]: received packet nr 4 type 6 len 28 
D, [2011-07-26T19:42:00.461260 #3511] DEBUG -- net.ssh.authentication.session[1475c10]: trying publickey 
D, [2011-07-26T19:42:00.461370 #3511] DEBUG -- net.ssh.authentication.agent[1470800]: connecting to ssh-agent 
E, [2011-07-26T19:42:00.461423 #3511] ERROR -- net.ssh.authentication.agent[1470800]: could not connect to ssh-agent 
D, [2011-07-26T19:42:00.461521 #3511] DEBUG -- net.ssh.authentication.methods.publickey[1470bfc]: trying publickey (d7:62:48:07:23:1d:c0:de:80:0d:27:a3:ab:92:72:be) 
D, [2011-07-26T19:42:00.461610 #3511] DEBUG -- tcpsocket[140aaf0]: queueing packet nr 5 type 50 len 364 
D, [2011-07-26T19:42:00.461648 #3511] DEBUG -- tcpsocket[140aaf0]: sent 388 bytes 
D, [2011-07-26T19:42:00.486881 #3511] DEBUG -- tcpsocket[140aaf0]: read 68 bytes 
D, [2011-07-26T19:42:00.487057 #3511] DEBUG -- tcpsocket[140aaf0]: received packet nr 5 type 51 len 44 
D, [2011-07-26T19:42:00.487186 #3511] DEBUG -- net.ssh.authentication.session[1475c10]: allowed methods: publickey,password 
D, [2011-07-26T19:42:00.487261 #3511] DEBUG -- net.ssh.authentication.session[1475c10]: trying password 
E, [2011-07-26T19:42:00.487346 #3511] ERROR -- net.ssh.authentication.session[1475c10]: all authorization methods failed (tried publickey, password) 

Cảm ơn

+0

Mã của bạn nên hoạt động, bạn có chắc là "path_to_private_key" là chính xác trong mã Ruby của bạn không? Đầu ra gỡ lỗi hiển thị như thế nào? –

+0

@ jeff-dallien Tôi đã thêm đầu ra cho câu hỏi. – grant

Trả lời

8
$ ssh-keygen -t rsa  
$ ssh-copy-id -i ~/.ssh/id_dsa.pub [email protected] 
$ ssh [email protected] 

require 'net/ssh' 
require 'logger' 

Net::SSH.start(
    'host', 'user', 
    :host_key => "ssh-rsa", 
    :encryption => "blowfish-cbc", 
    :keys => [ "~/.ssh/id_rsa.pub" ], 
    :compression => "zlib" 
) do |session| 
    ... 
end 

Đó phải giúp bạn!

+0

Cảm ơn, nhưng vào thời điểm câu hỏi này đã được trả lời, tôi đã từ lâu di chuyển qua vấn đề này, và quên ngay cả khi tôi hỏi nó :) – grant

+4

Tại sao điều này làm việc nhưng không phải là bài viết gốc? Có chỉ định host_key, mã hóa hoặc nén không, và nếu có thì tại sao? –

+4

Bằng cách này, các tùy chọn khóa trong ví dụ trên ngụ ý rằng nó đang tìm kiếm khóa công khai, nó phải là khóa riêng. –

Các vấn đề liên quan