无法使用 Ruby 和 net/ssh 密钥进行连接
我在使用 'net-ssh' gem 通过 ssh 从 ruby 进行连接时遇到问题,得到 Net::SSH::AuthenticationFailed。代码如下
require 'net/ssh'
keys = ["path_to_private_key"]
Net::SSH.start('host', 'user',:keys => keys, :verbose => :debug) do |ssh|
#ssh code
end
直接从命令行使用 ssh 可以工作:
ssh -i <path_to_private_key> user@host
我的 ssh API 错误吗?我尝试过“user@host”和“user”作为用户名,但结果相同。
这是调试输出:
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 `user@host'
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)
谢谢
I'm having trouble connecting via ssh from ruby using the 'net-ssh' gem, getting Net::SSH::AuthenticationFailed. The code is below
require 'net/ssh'
keys = ["path_to_private_key"]
Net::SSH.start('host', 'user',:keys => keys, :verbose => :debug) do |ssh|
#ssh code
end
Using ssh directly from the command line works:
ssh -i <path_to_private_key> user@host
Do I have the the ssh API wrong? I have tried both 'user@host' and just 'user' as the username with the same result.
Here is the debugging output:
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 `user@host'
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)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那一定对你有帮助!
That must help you!
请查看您的错误消息,
请注意表示错误的 E。
Please see your error message
Notice the E denoting the error.