简体   繁体   English

Java-使用密码的ProcessBuilder rsync命令失败

[英]Java - ProcessBuilder rsync command with password fails

I am trying to transfer a file via rsync. 我正在尝试通过rsync传输文件。

Here is my snippet: 这是我的片段:

ProcessBuilder builder = new ProcessBuilder("rsync", "-az", "-e", "ssh -q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no", username + "@" + host + ":" + filePath, destPath);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();

// Password
OutputStream outputStream = process.getOutputStream();
outputStream.write(password.getBytes(StandardCharsets.UTF_8));

// Wait for execution
int exitValue = process.waitFor();
if (exitValue != 0) {
    String errorMessage = read(errorStream);
    System.out.println(errorMessage);
}

and this is the error message: 这是错误消息:

rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.0]

I can confirm method arguments (such as password, username, host etc) are non-null and valid. 我可以确认方法参数(例如密码,用户名,主机等)为非null且有效。 Also I can successfully run the same rsync command using the CLI. 我也可以使用CLI成功运行相同的rsync命令。

I've found a similar problem here and tried SSH options with or without quotes but it didn't work. 我在这里发现了类似的问题并尝试使用带或不带引号的SSH选项,但是它不起作用。

Here is the output of ps aux | grep rsync 这是ps aux | grep rsync的输出ps aux | grep rsync ps aux | grep rsync while running the program: ps aux | grep rsync运行程序时:

emre      8566  0.0  0.0  17396   908 ?        S    18:15   0:00 rsync -az -e ssh -q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no user@XXXX:/tmp/dummy-file /tmp/
emre      8568  0.3  0.0  36148  3028 ?        S    18:15   0:00 ssh -q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no -l user XXXX rsync --server --sender -logDtprze.iLs . /tmp/dummy-file

What am I missing here? 我在这里想念什么? Is this the correct way to use ProcessBuilder ? 这是使用ProcessBuilder的正确方法吗?

Thanks in advance 提前致谢

Rsync doesn't handle authentication by itself, ssh-agent does this instead. Rsync不会自行处理身份验证,而是由ssh-agent进行。 Writing password to process output stream is wrong: it doesn't pass password to ssh-agent. 将密码写入进程输出流是错误的:它不会将密码传递给ssh-agent。 Instead you can set environment variable RSYNC_PASSWORD=<password> . 相反,您可以设置环境变量RSYNC_PASSWORD=<password>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM