我能够使用 JSch 成功连接到远程主机,但是,IT 团队在该主机上设置了一个提示,带有一些一般警告消息,例如“这是一个私有(private)系统,不允许未经授权的访问"等等。我必须手动按 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);
这是 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 通过将其传递给 showMessage
method 来处理它你对 UserInfo
interface 的实现.
您对该方法的实现很可能就是显示提示的内容。以您喜欢的方式更改实现。尽管确保您仍然合理地处理消息,比如记录它。由于该方法用于其他重要消息。
另一种可能是您看到的是一条没有提示的键盘交互消息。该消息由 UIKeyboardInteractive
interface 处理及其 UIKeyboardInteractive.promptKeyboardInteractive
method .
这两种方法都由您的 MyUserInfo
类实现以显示一些 GUI。
关于java - 使用 JSch ssh 到远程服务器,但在处理消息提示时卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49777273/