用户信息传递“是”点击即可运行

发布于 2024-10-31 06:14:13 字数 703 浏览 0 评论 0原文

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

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");

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");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2024-11-07 06:14:13

我们使用下面的代码:

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

We use the code below:

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();

and most importantly:

/**
 * 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) {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文