使用 JSch ssh 到远程服务器,但在处理消息提示时卡住了

Using JSch to ssh to a remote server but got stuck in dealing with the message prompt

我能够使用 JSch 成功连接到远程主机,但是,IT 团队在该主机上设置了一个提示,带有一些一般警告消息,例如 "This is a private system, unauthorized access is not permitted" 等。我有手动按 ENTER 键继续下一步。我尝试使用 Robot 自动执行此按键操作,但这似乎不起作用。提示需要几秒钟才能出现,所以我使用 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);

这是我的用户信息 class:

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 通过将其传递给 showMessage method of your implementation of UserInfo interface 来处理它。

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

另一种可能是您看到的 keyboard-interactive 消息没有任何提示。该消息由 UIKeyboardInteractive interface and its UIKeyboardInteractive.promptKeyboardInteractive method.

处理

这两种方法都是由您的 MyUserInfo class 实现的,以显示一些 GUI。

详情见How to read the SSH key-sig pair banner (for generating SSH password) after connecting to host in java?