ruby-on-railsnet-ssh

How to check SSH connection with Rails 5.2 and Net::SSH.start


I need to check whether the SSH connection to a remote server I use is correct or not.

I issue the following test:

  begin
    session = Net::SSH.start(uri, login, password: password)
    session.close
    Rails.logger.interactions.info '      Successful SSH connection'
  rescue
    Rails.logger.interactions.info '      Unable to connect via SSH'
  end

When the uri is incorrect (the server is unknonw), the rescue branch is invoked as expected.

But if the credentials are incorrect, the server console switches to interactive mode, and claims for a password:

a80838986@L821000109918A.ch's password:

And does not release until something is entered.

How to avoid this and jump to the exception instead?

Thanks a lot!


Solution

  • The non_interactive option of the start method avoids switching to interactive request for new credentials.

    The following statement solved my issue:

    session = Net::SSH.start(uri, login, password: password, non_interactive: true)