gpt4 book ai didi

java - UserInfo 传递 yes click to function

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:28:51 25 4
gpt4 key购买 nike

我正在使用 ssh 工具与学校服务器建立 ssh 连接,我正在使用源中的示例建立 ssh 连接。我的问题是我不想要任何用户输入提示,但是会弹出一个提示,提示无法确定主机的真实性,用户需要按"is"才能继续,我如何编写代码让程序接受提示本身。

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) {
}
}

关于java - UserInfo 传递 yes click to function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5561862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com