java - “远程确认失败:scp:模糊目标”,同时使用 Windows 机器上的“scp”上传文件,而它在 Linux 和 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)上运行。
我无法弄清楚确切的问题以及如何解决它。
任何人都可以建议从中恢复过来吗?
谢谢
解决方案
远程路径必须是/
,不是\
。
并且参数 tocreateCopyCommand
不能Path
像在 Windows 上那样将转换/
为\
.
推荐阅读
- kubernetes - 是否可以将多个入口与 IBM Cloud Kubernetes 服务合并?
- r - 从嵌套循环中保存图形和统计信息
- javascript - 从输入字段拍照并转换为base64
- django - 将用户模型中的 first_name、last_name 和 email 设置为字段中的默认值
- sql - 使用拆分的 T-SQL 分组
- python - 输出是数字列表而不是一个数字
- java - 具有该位置 [1] 的参数不存在;嵌套异常是 java.lang.IllegalArgumentException:具有该位置 [1] 的参数不存在
- python - 在 SQLAlchemy 中使用 selectinload 加载相关对象时的排序顺序
- asp.net - 要并排渲染的 CommandField ImageButtons
- java - 为什么在减少之前需要对分区进行排序?