SSH_MSG_DISCONNECT: 1 ssh disconnect host not allowed to connect

  Kiến thức lập trình

I’m upgrading my SFTP client library from com.jcraft.jsch 0.1.55 to com.github.mwiede.jsch 0.2.17 to utilize OpenSSH keys.
My previous code using com.jcraft.jsch 0.1.55 was working fine by providing the “RSA PRIVATE KEY”. (I understand that SFTP servers do not share their private key). But I was able to connect like that. while it’s now failing.

  private void connectAndDo(
      CitySftpConfigurationDTO sftpConfiguration, Consumer<ChannelSftp> action)
      throws SftpConnectionException {
    try {
      retryTemplate.execute(
          retryContext -> {
            try {
               Path tempFile = Files.createTempFile("private_sftp_key", "");
               Files.write(tempFile, sftpConfiguration.getPrivateKey().getBytes());
               jsch.addIdentity(tempFile.toString());
               Files.deleteIfExists(tempFile);
            } catch (IOException e) {
                throw new ShouldNotHappenException("Unable to create file for private key");
            }
            Session session =
                jsch.getSession(
                    sftpConfiguration.getUsername(),
                    sftpConfiguration.getHost(),
                    sftpConfiguration.getPort());
            if (!proxyHost.equals("") && proxyPort != 0) {
              session.setProxy(new ProxyHTTP(proxyHost, proxyPort));
            }
            session.setConfig("StrictHostKeyChecking", "no");

            /* added for com.github.mwiede.jsch 0.2.17 
            session.setConfig("server_host_key", 
                session.getConfig("server_host_key") + ",ssh-rsa,ssh-dss");
            session.setConfig("PubkeyAcceptedAlgorithms",
                session.getConfig("PubkeyAcceptedAlgorithms") + ",ssh-rsa,ssh-dss");
            */ 

            try {
              session.connect();
              Channel channel = session.openChannel("sftp");
              ChannelSftp channelSftp = (ChannelSftp) channel;
              try {
                channelSftp.connect();
                action.accept(channelSftp);
              } finally {
                channelSftp.disconnect();
              }
            } finally {
              session.disconnect();
            }
            return null;
          });
    } catch (JSchException e) {
      throw new SftpConnectionException(CONNECTION_ERROR_MESSAGE, e);
    }
  }

the error code:

Caused by: com.jcraft.jsch.JSchSessionDisconnectException: SSH_MSG_DISCONNECT: 1 ssh disconnect host not allowed to connect 
    at com.jcraft.jsch.Session.read(Session.java:1316)
    at com.jcraft.jsch.UserAuthPublicKey._start(UserAuthPublicKey.java:325)
    at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119)
    at com.jcraft.jsch.Session.connect(Session.java:479)
    at com.jcraft.jsch.Session.connect(Session.java:198)

I’m expecting to use the second version with the same code

LEAVE A COMMENT