2012-03-06 76 views
3

我试图通过SSH使用Ruby连接服务器,但是我有Net :: SSH :: AuthenticationFailed错误。问题是它在PHP中起作用。Ruby ssh错误Net :: SSH :: AuthenticationFailed虽然它与PHP一起工作

这里是我的Ruby代码:

require 'rubygems' 
require 'net/ssh' 
include Net 
domain = 'ks2.so.d-sites.com' # insert IP address or domain name here 
begin 
Net::SSH.start(domain, 'webistrano', :verbose => :debug, :keys => ['/home/webistrano/.ssh/id_rsa', '/home/webistrano/.ssh/id_rsa.pub'], :port => 22) do |ssh| 
print "connected" 
end 
rescue Net::SSH::HostKeyMismatch => e 
    puts "remembering new key: #{e.fingerprint}" 
    e.remember_host! 
    retry 
end 

我得到这个错误:

D, [2012-03-06T20:40:02.974862 #24165] DEBUG -- net.ssh.transport.session[3f8c5d3a06a0]: establishing connection to ks2.so.d-sites.com:22 
D, [2012-03-06T20:40:02.975912 #24165] DEBUG -- net.ssh.transport.session[3f8c5d3a06a0]: connection established 
I, [2012-03-06T20:40:02.976039 #24165] INFO -- net.ssh.transport.server_version[3f8c5d39edc8]: negotiating protocol version 
D, [2012-03-06T20:40:02.981160 #24165] DEBUG -- net.ssh.transport.server_version[3f8c5d39edc8]: remote is `SSH-2.0-OpenSSH_5.8p1-hpn13v10' 
D, [2012-03-06T20:40:02.981231 #24165] DEBUG -- net.ssh.transport.server_version[3f8c5d39edc8]: local is `SSH-2.0-Ruby/Net::SSH_2.3.0 x86_64-linux' 
D, [2012-03-06T20:40:02.982272 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: read 856 bytes 
D, [2012-03-06T20:40:02.982418 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: received packet nr 0 type 20 len 852 
I, [2012-03-06T20:40:02.982517 #24165] INFO -- net.ssh.transport.algorithms[3f8c5d39e418]: got KEXINIT from server 
I, [2012-03-06T20:40:02.982690 #24165] INFO -- net.ssh.transport.algorithms[3f8c5d39e418]: sending KEXINIT 
D, [2012-03-06T20:40:02.982883 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: queueing packet nr 0 type 20 len 716 
D, [2012-03-06T20:40:02.982960 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: sent 720 bytes 
I, [2012-03-06T20:40:02.983003 #24165] INFO -- net.ssh.transport.algorithms[3f8c5d39e418]: negotiating algorithms 
D, [2012-03-06T20:40:02.983161 #24165] DEBUG -- net.ssh.transport.algorithms[3f8c5d39e418]: 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, [2012-03-06T20:40:02.983207 #24165] DEBUG -- net.ssh.transport.algorithms[3f8c5d39e418]: exchanging keys 
D, [2012-03-06T20:40:02.983416 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: queueing packet nr 1 type 34 len 20 
D, [2012-03-06T20:40:02.983469 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: sent 24 bytes 
D, [2012-03-06T20:40:03.024146 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: read 152 bytes 
D, [2012-03-06T20:40:03.024270 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: received packet nr 1 type 31 len 148 
D, [2012-03-06T20:40:03.027331 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: queueing packet nr 2 type 32 len 140 
D, [2012-03-06T20:40:03.027409 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: sent 144 bytes 
D, [2012-03-06T20:40:03.032446 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: read 720 bytes 
D, [2012-03-06T20:40:03.032563 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: received packet nr 2 type 33 len 700 
D, [2012-03-06T20:40:03.035433 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: queueing packet nr 3 type 21 len 20 
D, [2012-03-06T20:40:03.035505 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: sent 24 bytes 
D, [2012-03-06T20:40:03.035617 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: received packet nr 3 type 21 len 12 
D, [2012-03-06T20:40:03.035954 #24165] DEBUG -- net.ssh.authentication.session[3f8c5e189fdc]: beginning authentication of `webistrano' 
D, [2012-03-06T20:40:03.036081 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: queueing packet nr 4 type 5 len 28 
D, [2012-03-06T20:40:03.036130 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: sent 52 bytes 
D, [2012-03-06T20:40:03.074777 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: read 52 bytes 
D, [2012-03-06T20:40:03.074912 #24165] DEBUG -- tcpsocket[3f8c5d39f250]: received packet nr 4 type 6 len 28 
D, [2012-03-06T20:40:03.075018 #24165] DEBUG -- net.ssh.authentication.session[3f8c5e189fdc]: trying password 
E, [2012-03-06T20:40:03.075077 #24165] ERROR -- net.ssh.authentication.session[3f8c5e189fdc]: all authorization methods failed (tried password) 
/usr/lib64/ruby/gems/1.8/gems/net-ssh-2.3.0/lib/net/ssh.rb:200:in `start': webistrano (Net::SSH::AuthenticationFailed) 
    from ssh-test.rb:6 

我写了一个简单的PHP代码,它的工作原理:

<?php 
$connection = ssh2_connect('ks2.so.d-sites.com', 22); 

if (ssh2_auth_pubkey_file($connection, 'webistrano', 
          '/home/webistrano/.ssh/id_rsa.pub', 
          '/home/webistrano/.ssh/id_rsa')) { 
    echo "Identification réussie en utilisant une clé publique\n"; 
} else { 
    die('Echec de l\'identification en utilisant une clé publique'); 
} 

是否有某人那可以帮助我? 非常感谢!

回答

4

我发现如何解决该问题:在Net::SSH.start函数中添加:auth_methods => ['publickey','password']参数。

+0

结帐也':non_interactive => true' – nhed 2018-03-09 22:20:54

0

我没有使用Net :: SSH.start解决方案,但选择了set :ssh_options, {:forward_agent => true, :auth_methods => 'publickey'}这似乎在我的工作。注意到最初,我只是在运行在FreeBSD,CentOS和MacOSX上的3台机器上使用了set:ssh_options,{:forward_agent => true},它运行良好 - 甚至在生产版本上使用,但是在基于VBoxed的基于Vagrant的CentOS(6.4)机器停止工作。所以,添加auth_method的确有窍门。