UserInfo传递是单击以执行功能 [英] UserInfo passing yes click to function
本文介绍了UserInfo传递是单击以执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用ssh工具与学校服务器建立ssh连接,我使用的示例是来自进行ssh连接的源代码的示例。我的问题是我不想要任何用户输入提示但是弹出提示说无法建立主机的真实性并且用户需要按是继续,我怎样才能为程序编码以接受提示本身。
I am using ssh tools to make a ssh connection to a school server, the example I am using an example from the source that makes an ssh connection. My problem is I don't want any user input prompts however a prompt pops up saying the authenticity of host can't be established and the user needs to press yes to continue, how can I code it for the program to accept the prompt by itself.
Session session = jsch.getSession(user, host, 22);
//username and host I can input directly into program so thats not a problem
// username and password will be given via UserInfo interface.
UserInfo ui = new MyUserInfo();
//this is the part that uses the UserInfo, which pulls up a prompt
//how can I code the prompt to automatically choose yes?
session.setUserInfo(ui);
session.setPassword("password");
session.connect();
String command = JOptionPane.showInputDialog("Enter command", "set|grep SSH");
推荐答案
我们使用以下代码:
try {
session = jsch.getSession(user, host, port);
}
catch (JSchException e) {
throw new TransferException("Failed to open session - " + params, e);
}
session.setPassword(password);
// Create UserInfo instance in order to support SFTP connection to any machine
// without a key username and password will be given via UserInfo interface.
UserInfo userInfo = new SftpUserInfo();
session.setUserInfo(userInfo);
try {
session.connect(connectTimeout);
}
catch (JSchException e) {
throw new TransferException("Failed to connect to session - " + params, e);
}
boolean isSessionConnected = session.isConnected();
最重要的是:
/**
* Implements UserInfo instance in order to support SFTP connection to any machine without a key.
*/
class SftpUserInfo implements UserInfo {
String password = null;
@Override
public String getPassphrase() {
return null;
}
@Override
public String getPassword() {
return password;
}
public void setPassword(String passwd) {
password = passwd;
}
@Override
public boolean promptPassphrase(String message) {
return false;
}
@Override
public boolean promptPassword(String message) {
return false;
}
@Override
public boolean promptYesNo(String message) {
return true;
}
@Override
public void showMessage(String message) {
}
}
这篇关于UserInfo传递是单击以执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文