Mr. Rails Mr. Rails - 1 year ago 151
Ruby Question

Ruby Net::SSH infinity wait for password

I have a simple functional for run some command via ssh, here is an example:

Net::SSH.start(, self.user, config: true, keys: [self.pem_key] ) do| ssh |
result = ssh.exec! self.command

It works OK except case when I forgot configure remote host (as example add key in ~/.ssh/authorized) and my app stuck on password request:

Completed 200 OK in 246ms (Views: 238.1ms | ActiveRecord: 4.3ms)

Started POST "/xxx" for ::1 at 2016-05-24 13:43:11 +0300
Processing by XXX::XXXController#run_now as JS
Parameters: {"id"=>"2"}
XXX::Check Load (0.3ms) SELECT ...
(0.1ms) BEGIN
SQL (0.3ms) UPDATE ...
(0.3ms) COMMIT
Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text.
some_user@some_host's password:

How I can set timeout for this case or how I can exclude this case at all (raise some error?)

Answer Source

According to the documentation, the Net::SSH.start method accepts a :non_interactive => true option which should make your test (code) fail instead of asking for a password interactively. Other, non-interactive authentication options, such as the private key, will of course still work.

So, try this:

Net::SSH.start(, self.user, config: true, non_interactive: true, keys: [self.pem_key]) do| ssh |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download