使用 JSch 通过 ssh 连接到远程服务器,但在处理消息提示时陷入困境



我能够使用 JSch 成功连接到远程主机,但是,IT 团队在该主机上设置了一个提示,其中包含一些一般警告消息,例如"这是一个私有系统,不允许未经授权的访问"等。 我必须手动按 ENTER 键才能继续。我尝试使用机器人自动按下此键,但这似乎不起作用。 提示需要几秒钟才能显示,所以我正在使用Thread.sleep(5000),我也尝试使用Robot.delay(5000)。 感谢任何指示。我已经尝试了有关此主题的任何现有答案,但找不到。

这是我的代码片段。

Session session = jsch.getSession("userName", "hostNmae", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("Password");
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.connect();
Thread.sleep(5000);
// Press ENTER
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

下面是 MyUserInfo 类:

public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
    public String getPassword(){ return passwd; }
    public boolean promptYesNo(String str){
        Object[] options={ "yes", "no" };
        int foo=JOptionPane.showOptionDialog(null,
                str,
                "Warning",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.WARNING_MESSAGE,
                null, options, options[0]);
        return foo==0;
    }
    String passwd;
    JTextField passwordField=(JTextField)new JPasswordField(20);
    public String getPassphrase(){ return null; }
    public boolean promptPassphrase(String message){ return true; }
    public boolean promptPassword(String message){
        Object[] ob={passwordField};
        int result=
                JOptionPane.showConfirmDialog(null, ob, message,
                        JOptionPane.OK_CANCEL_OPTION);
        if(result==JOptionPane.OK_OPTION){
            passwd=passwordField.getText();
            return true;
        }
        else{ return false; }
    }
    public void showMessage(String message){
        JOptionPane.showMessageDialog(null, message);
    }
    final GridBagConstraints gbc =
            new GridBagConstraints(0,0,1,1,1,1,
                    GridBagConstraints.NORTHWEST,
                    GridBagConstraints.NONE,
                    new Insets(0,0,0,0),0,0);
    private Container panel;
    public String[] promptKeyboardInteractive(String destination,
                                              String name,
                                              String instruction,
                                              String[] prompt,
                                              boolean[] echo){
        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        gbc.weightx = 1.0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridx = 0;
        panel.add(new JLabel(instruction), gbc);
        gbc.gridy++;
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        JTextField[] texts=new JTextField[prompt.length];
        for(int i=0; i<prompt.length; i++){
            gbc.fill = GridBagConstraints.NONE;
            gbc.gridx = 0;
            gbc.weightx = 1;
            panel.add(new JLabel(prompt[i]),gbc);
            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weighty = 1;
            if(echo[i]){
                texts[i]=new JTextField(20);
            }
            else{
                texts[i]=new JPasswordField(20);
            }
            panel.add(texts[i], gbc);
            gbc.gridy++;
        }
        if(JOptionPane.showConfirmDialog(null, panel,
                destination+": "+name,
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE)
                ==JOptionPane.OK_OPTION){
            String[] response=new String[prompt.length];
            for(int i=0; i<prompt.length; i++){
                response[i]=texts[i].getText();
            }
            return response;
        }
        else{
            return null;  // cancel
        }
    }
}

您看到的很可能是SSH身份验证横幅。

默认情况下,JSch 通过将它传递给UserInfo接口实现showMessage方法来处理它。

该方法的实现很可能是显示提示的内容。按照您喜欢的方式更改实现。尽管请确保您仍然合理地处理消息,例如记录它。由于该方法用于其他重要消息。

另一种可能性是您看到没有提示的键盘交互消息。该消息由UIKeyboardInteractive接口及其UIKeyboardInteractive.promptKeyboardInteractive方法处理。

这两种方法都由 MyUserInfo实现以显示一些 GUI。

有关详细信息,请参阅在 Java 中连接到主机后如何读取 SSH 密钥签名对横幅(用于生成 SSH 密码(?

最新更新