Permalink
Browse files

Merge pull request #20 from drichert/feature/no-password-prompt

Feature/no password prompt
  • Loading branch information...
2 parents d2b2c1f + eaf8a5e commit 598d6656c72633c917e5cecbfc37f8896becd60d @delano committed Aug 15, 2012
Showing with 26 additions and 9 deletions.
  1. +6 −0 README.rdoc
  2. +7 −4 lib/rye/box.rb
  3. +13 −5 try/14_auth_methods_tryouts.rb
View
@@ -165,6 +165,12 @@ So, the hostname 'filibuster' is used from 'firewall.lan'
rset.uptime
# => [[17:17:44 up 548 days, 13:37, 20 users, load average: 0.12, 0.07, 0.06], [01:17:49 up 6 days, 1:39, 9 users, load average: 0.13, 0.09, 0.09]]
+== Example 9 -- Disable password prompt
+
+If you're running in a terminal but you want <tt>Net::SSH::AuthenticationFailed</tt> to be raised instead of getting a password prompt when authentication fails, set <tt>:password_prompt</tt> option to false:
+
+ rbox = Rye::Box.new("foo.com", :user => "dan", :password => "inkorrect", :password_prompt => false)
+ rbox.uptime # => raises Net::SSH::AuthenticationFailed
== About Safe-Mode
View
@@ -106,6 +106,7 @@ def exception_hook=(val); @rye_exception_hook = val; end
# * :password => the user's password (ignored if there's a valid private key)
# * :templates => the template engine to use for uploaded files. One of: :erb (default)
# * :sudo => Run all commands via sudo (default: false)
+ # * :password_prompt => Show a password prompt on auth failure (default: true)
#
# NOTE: +opts+ can also contain any parameter supported by
# Net::SSH.start that is not already mentioned above.
@@ -132,7 +133,8 @@ def initialize(host='localhost', opts={})
:error => STDERR,
:getenv => true,
:templates => :erb,
- :quiet => false
+ :quiet => false,
+ :password_prompt => true
}.merge(opts)
# Close the SSH session before Ruby exits. This will do nothing
@@ -150,8 +152,9 @@ def initialize(host='localhost', opts={})
@rye_ostype, @rye_impltype = @rye_opts.delete(:ostype), @rye_opts.delete(:impltype)
@rye_quiet, @rye_sudo = @rye_opts.delete(:quiet), @rye_opts.delete(:sudo)
@rye_templates = @rye_opts.delete(:templates)
-
- # Store the state of the terminal
+ @rye_password_prompt = @rye_opts.delete(:password_prompt)
+
+ # Store the state of the terminal
@rye_stty_save = `stty -g`.chomp rescue nil
unless @rye_templates.nil?
@@ -684,7 +687,7 @@ def connect(reconnect=true)
# only auth method
if @rye_opts[:auth_methods] == ["publickey"]
raise Net::SSH::AuthenticationFailed
- elsif STDIN.tty? && retried <= 3
+ elsif @rye_password_prompt && (STDIN.tty? && retried <= 3)
STDERR.puts "Passwordless login failed for #{@rye_user}"
@rye_opts[:password] = highline.ask("Password: ") { |q| q.echo = '' }.strip
@rye_opts[:auth_methods].push *['keyboard-interactive', 'password']
@@ -1,13 +1,12 @@
require "rye"
-## Don't prompt for password if "publickey" is the only :auth_method
-
-# May need to update this in the future with a
+# May need to update this in the future with a
# different free SSH provider
-hostname = "shellmix.com"
+@hostname = "shellmix.com"
+## Don't prompt for password if "publickey" is the only :auth_method
box = Rye::Box.new(
- hostname,
+ @hostname,
:auth_methods => ["publickey"]
)
@@ -18,3 +17,12 @@
end
#=> Net::SSH::AuthenticationFailed
+## Never prompt for password if :no_password_prompt option is true
+box = Rye::Box.new(@hostname, :password_prompt => false)
+
+begin
+ box.connect
+rescue Net::SSH::AuthenticationFailed => ex
+ ex.class
+end
+#=> Net::SSH::AuthenticationFailed

0 comments on commit 598d665

Please sign in to comment.