“Remote Acknowledge failed: scp: ambiguous target” while uploading files using “scp” from Windows machine, while it works on Linux and Mac

I am trying to run a test case which basically copies a file from my machine to a mock server running in docker. The same test works fine on Mac and Ubuntu. But on Windows it’s getting failed with the following error:-

Exception while executing command on remote SSH server
java.lang.RuntimeException: Exception while executing command on remote SSH server
    at com.org.implementation.cboserver.RemoteCommandWrapper.runSshCommandWithTimeout(RemoteCommandWrapper.java:53)
    at com.org.implementation.cboserver.RemoteCommandWrapper.copyLocalToRemote(RemoteCommandWrapper.java:121)
...
    
Caused by: java.util.concurrent.ExecutionException: java.lang.RuntimeException: Remote Acknowledge failed: scp: ambiguous target

    at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)
    at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2022)
    at com.org.implementation.cboserver.RemoteCommandWrapper.runSshCommandWithTimeout(RemoteCommandWrapper.java:49)
    ... 92 more
Caused by: java.lang.RuntimeException: Remote Acknowledge failed: scp: ambiguous target

    at com.org.implementation.cboserver.RemoteCommandWrapper.checkAck(RemoteCommandWrapper.java:174)
    at com.org.implementation.cboserver.RemoteCommandWrapper.waitForFileCopied(RemoteCommandWrapper.java:136)
    at com.org.implementation.cboserver.RemoteCommandWrapper.lambda$copyLocalToRemote$1(RemoteCommandWrapper.java:124)
    at com.org.implementation.cboserver.RemoteCommandWrapper.tryToExecuteCommand(RemoteCommandWrapper.java:68)
    at com.org.implementation.cboserver.RemoteCommandWrapper.lambda$runSshCommandWithTimeout$0(RemoteCommandWrapper.java:45)
...

RemoteCommandWrapper.java:-

public void copyLocalToRemote(Session session, Path localPath, Path remotePath, String fileName) {
    log.info("localPath: {}",localPath); //localPath:C:Usersmyuserproject1srctestresources5f9cce18-362e-4973-a26b-730131841d7b
    log.info("remotePath: {}",remotePath); // remotePath:
    log.info("fileName: {}",fileName);// fileName: CustomFile.zip
    String copyCommand = createCopyCommand(remotePath);
    runSshCommandWithTimeout(session,
            copyCommand,
            (channel, in, out) -> {
                waitForFileCopied(channel, in, out, localPath, fileName);
                return null;
            });
}

private String createCopyCommand(Path remotePath) {
    return String.format("scp -p -t %sn", remotePath);
}

private void waitForFileCopied(Channel channel, InputStream in, OutputStream out, Path localPath, String fileName) {
    Path from = localPath.resolve(fileName);
    try (FileInputStream fis = new FileInputStream(from.toString())) {
        checkAck(in);
        setFileAccess(from, out, in);
        fis.transferTo(out);

        // send ''
        out.write(new byte[]{0}, 0, 1);
        out.flush();
        checkAck(in);
    } catch (IOException e) {
        throw new RuntimeException("Error copying custom file", e);
    }
}

@SneakyThrows
private void checkAck(InputStream in){
    int b = in.read();
    if (b == 0) return;

    if (b < 0) throw new RuntimeException("Remote Acknowledge failed: " + Integer.valueOf(b));

    StringBuilder sb = new StringBuilder();
    int c;
    do {
        c = in.read();
        sb.append((char) c);
    }
    while (c != 'n');
    throw new RuntimeException("Remote Acknowledge failed: " + sb.toString());
}

<T> T runSshCommandWithTimeout(final Session session, String command, RemoteResultFactory<T> resultFactory) {
    CompletableFuture<T> future = CompletableFuture.supplyAsync(
            () -> tryToExecuteCommand(session, command, resultFactory)
    );

    try {
        return future.get(timeoutMs, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        throw new RemoteTimeoutException(String.format("Timeout while executing command on remote SSH server: %d ms", timeoutMs));
    } catch (Exception e) {
        throw new RuntimeException("Exception while executing command on remote SSH server", e);
    }
}

private <T> T tryToExecuteCommand(Session session, String command, RemoteResultFactory<T> resultFactory) {
    Channel channel = null;
    try {
        channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);

        // TODO handle extInputStream (error stream)
        try (InputStream in = channel.getInputStream();
             OutputStream out = channel.getOutputStream()) {

            channel.connect();
            return resultFactory.getResult(channel, in, out);
        }
    } catch (JSchException | IOException e) {
        throw new RuntimeException("Exception while executing command on remote SSH server", e);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
        session.disconnect();
    }
}

While searching this error on Google I can understand that scp is not able to recognize the target destination and that is also should be problem with Window paths or may be because of "/" or "" as the same test is working on Linux based filesystems (and in Mac).

I am not able to figure out the exact issue and how to resolve it.

Can anybody suggest something to recover from this?

Thanks

Answer

The remote path must be /, not .

And the argument to createCopyCommand cannot be Path, as on Windows, that will translate the / to .

Leave a Reply

Your email address will not be published. Required fields are marked *