2016-03-27 96 views
0

我有这个功能,当我传递正确的用户名和密码时,以及当我故意传递错误的密码时,我卡在Password:提示符下。Net :: SSH卡住了密码提示

def perform(sensor_params) 
    @hostname = "10.0.10.100" 
    @username = "root" 
    @password = "root" 

    @cmd = "id" 

    begin 
     ssh = Net::SSH.start(@hostname, @username, :password => @password) 
     res = ssh.exec!(@cmd) 
     ssh.close 
     puts res 
    rescue 
     puts "Unable to connect to #{@hostname} using #{@username}/#{@password}" 
    end 
end 

任何想法,当我传递的密码不正确时,只能吐出消息“无法连接”?

感谢

+0

缺少密码命令的双引号。 –

+0

不知道你的目标是“我怎么只是吐出信息”。使用'non_interactive'设置并移除'rescue'块(或缩小它),该方法应该引发'Net :: SSH :: AuthenticationFailed'异常。 –

回答

4

根据该文档,你可以通过:non_interactive => true,而不是抛出一个身份验证错误。

+0

我实际上已经尝试过使用:non_interactive => true,当我这样做时,我一直在收到认证错误,即使我正在传递正确的密码 – Deano

0

通过:number_of_password_prompts => 0解决了我的问题。

+0

这与奇怪地等同于设置['non_interactive'](https://github.com /net-ssh/net-ssh/blob/master/lib/net/ssh.rb#L212),如Frederick Cheung所述。 –