如何检查SSH连接5.2和NET :: SSH。

发布于 2025-02-06 03:06:27 字数 643 浏览 1 评论 0原文

我需要检查与我使用的远程服务器的SSH连接是否正确。

我发出以下测试:

  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

当URI不正确(服务器未开头)时,将按预期调用救援分支。

但是,如果凭据不正确,服务器控制台将切换到交互式模式,并要求密码:

[email protected]'s password:

并且在输入某些内容之前不会发布。

如何避免这种情况并跳到例外?

多谢!

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:

[email protected]'s password:

And does not release until something is entered.

How to avoid this and jump to the exception instead?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2025-02-13 03:06:27

non_interactive start 方法的选项避免切换到新凭据的交互式请求。

以下声明解决了我的问题:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文