【问题标题】:How to automate connect to a ssh server with keyboard-interactive authentication using jsch lib in java [duplicate]如何在java中使用jsch lib通过键盘交互式身份验证自动连接到ssh服务器[重复]
【发布时间】:2020-10-15 14:18:57
【问题描述】:

我想使用 jsch 自动连接到具有键盘交互式身份验证(或质询-响应身份验证)的 ssh 服务器。 我已经像这样设置了用户信息和配置。

session.setUserInfo(myUserInfo);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications",
                    "publickey,keyboard-interactive,password");

myUserInfo 对象实现UserInfo interface 当我调试源代码时,我总是得到一个 com.jcraft.jsch.JSchException: Auth fail 异常。 有人说 myUserInfo 应该实现UIKeyboardInteractive。 但我无法理解官方的 JSch UserAuthKI 示例。理解起来太复杂了。 我只想让连接自动化。 谁能给我一个简单的例子,拜托。 谢谢。

【问题讨论】:

  • 它没有回答您的问题,但在自动化 ssh 连接时,最好使用公钥身份验证。

标签: java ssh jsch


【解决方案1】:

我找到了解决方案。 让我们的 myUserInfo 类实现 UIKeyboardInteractive 接口。 并像这样重写了 promptKeyboardInteractive 方法。

@Override
public String[] promptKeyboardInteractive(String destination, String name,
                                              String instruction, String[] prompt,
                                              boolean[] echo)
{
       System.out.println("#####promptKeyboardInteractive#####");
       System.out.println("destination: " + destination);
       System.out.println("name: " + name);
       System.out.println("instruction: " + instruction);
       System.out.println("prompt.length: " + prompt.length);
       System.out.println("prompt[0]: " + prompt[0]);
       System.out.println("echo[0]: " + echo[0]);
       String[] response=new String[prompt.length];
       response[0] = passwd;
       return response;
}

这是有效的。我可以从 ssh 服务器获得连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2022-09-27
    • 2017-12-10
    相关资源
    最近更新 更多