![](/img/trans.png)
[英]pickup latest files from a windows directory and scp to linux server
[英]“Remote Acknowledge failed: scp: ambiguous target” while uploading files using “scp” from Windows machine, while it works on Linux and Mac
我正在嘗試運行一個測試用例,它基本上將文件從我的機器復制到在 docker 中運行的模擬服務器。 相同的測試在 Mac 和 Ubuntu 上運行良好。 但是在 Windows 上它失敗並出現以下錯誤:-
Exception while executing command on remote SSH server
java.lang.RuntimeException: Exception while executing command on remote SSH server
at com.org.implementation.RemoteCommandWrapper.runSshCommandWithTimeout(RemoteCommandWrapper.java:53)
at com.org.implementation.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.RemoteCommandWrapper.checkAck(RemoteCommandWrapper.java:174)
at com.org.implementation.RemoteCommandWrapper.waitForFileCopied(RemoteCommandWrapper.java:136)
at com.org.implementation.RemoteCommandWrapper.lambda$copyLocalToRemote$1(RemoteCommandWrapper.java:124)
at com.org.implementation.RemoteCommandWrapper.tryToExecuteCommand(RemoteCommandWrapper.java:68)
at com.org.implementation.RemoteCommandWrapper.lambda$runSshCommandWithTimeout$0(RemoteCommandWrapper.java:45)
...
RemoteCommand.java:-
public void copyLocalToRemote(Session session, Path localPath, Path remotePath, String fileName) {
log.info("localPath: {}",localPath); //localPath:C:\Users\myuser\project1\src\test\resources\5f9cce18-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 %s\n", 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 '\0'
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();
}
}
在 Google 上搜索此錯誤時,我可以理解scp
無法識別目標目的地,這也應該是 Window 路徑的問題,或者可能是因為"/"
或"\"
,因為相同的測試正在基於 Linux文件系統(以及在 Mac 中)。
我無法弄清楚確切的問題以及如何解決它。
任何人都可以建議從中恢復過來嗎?
謝謝
遠程路徑必須是/
,而不是\
。
並且createCopyCommand
的參數不能是Path
,就像在 Windows 上一樣,這會將/
轉換為\
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.