UserInfo passing yes click to function

后端 未结 1 1908
情话喂你
情话喂你 2021-01-05 09:30

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 wan

相关标签:
1条回答
  • 2021-01-05 09:38

    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) {
        }
    }
    
    0 讨论(0)
提交回复
热议问题