在 Windows 上从 java 调用 rsync+ssh 的问题
我在安装了 cygwin 的 Windows Vista 上从 java 调用 rsync 时遇到问题。 奇怪的是,将完全相同的命令粘贴到命令 shell 中效果很好。
我的测试 java 调用如下所示。
String[] envVars = {"PATH=c:/cygwin/bin;%PATH%"};
File workingDir = new File("c:/cygwin/bin/");
Process p = Runtime.getRuntime().exec("c:/cygwin/bin/rsync.exe -verbose -r -t -v --progress -e ssh /cygdrive/c/Users/dokeeffe/workspace/jrsync/ www.servername.net:/home/dokeeffe/rsync/",envVars,workingDir);
然后我启动 2 个流读取器线程来捕获并记录 Process p 的 inputStream 和 errorStream。
这是输出......
DEBUG: com.mddc.client.rsync.StreamGobbler - opening connection using: ssh www.servername.net rsync --server -vvtre.iLs . /home/dokeeffe/rsync/
DEBUG: com.mddc.client.rsync.StreamGobbler - rsync: pipe: Operation not permitted (1)
DEBUG: com.mddc.client.rsync.StreamGobbler - rsync error: error in IPC code (code 14) at /home/lapo/packaging/rsync-3.0.4-1/src/rsync-3.0.4/pipe.c(57) [sender=3.0.4]
发生错误的 rsync 代码是 pipeline.c(57) ,就是这个。
if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
rsyserr(FERROR, errno, "pipe");
exit_cleanup(RERR_IPC);
}
因此,由于某种原因 fd_pair(to_child_pipe) 是 < 0 或 fd_pair(from_child_pipe) < 0.
如果有人有任何建议那就太好了,因为我现在陷入困境。 谢谢,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
cygwin 的 rsync 二进制发行版可以识别 ssh 命令吗? 当您放入 shell 中时,您尝试执行的命令可能工作正常,因为像 bash 这样的东西知道如何解释 ssh 命令来建立连接,即使用 ssh 程序为 rsync 程序创建连接。 根据您的调试信息,我认为 rsync 正在等待连接,但收到了一个无法通过管道传输的字符串“ssh ...”。
为了解决这个问题,你必须让 bash 为你做这些工作。 请参阅此页面中有关照顾孩子的线程部分。 您需要分叉命令 shell 的实例并将命令写入 shell 子进程。 所以像这样:
您可能也想读取进程输出,因为如果您不读取它,它将停止执行。 提供的链接很好地涵盖了该主题。
Does cygwin's binary distribution of rsync recognize the ssh command? The command you are trying to execute probably works fine when you put into the shell because something like bash knows how to interpret the ssh command to establish a connection, i.e. use the ssh program to create a connection for the rsync program. From your debugging information I think rsync is expecting a connection but instead receives a string "ssh ..." which it fails to pipe.
To get around this you have to let bash do the work for you. Look at this page in the section on threads to tend the child. You would need to fork an instance of a command shell and write your command to the shell subprocess. So something like this:
You would probably want to read the processes output too because it will stop executing if you do not read it. The link provided covers the topic well.
您是否在某个地方忘记了 ssh 私钥/公钥?
Do you have ssh private/public keystore somewhere that you have forgotten about?
我从未尝试过使用不同的线程来读取/写入,仅尝试使用执行命令的同一线程。 这可能会导致您的问题吗?
您还可以尝试将 -v 传递给 ssh 以获取调试输出。 这在过去帮助我解决了 ssh 连接问题。 您将传递
-e "ssh -v"
。I've never tried reading/writing using a different thread, only the same thread that executed the command. Could this be causing your problem?
You can also try passing -v to ssh to get debug output. That has helped me in the past to resolve ssh connection problems. You would pass
-e "ssh -v"
.谢谢大家,尤其是丹尼尔。 你发给我的链接很棒。
最后我使用 ProcessBuilder 而不是 Runtime.exec。
这是代码,如果它对任何人有帮助......
Thanks everyone esp Daniel. The link you sent me is great.
In the end I used ProcessBuilder instead of Runtime.exec.
Here is the code if its any help to anyone.....
我遇到类似问题的经验:
我的常量:
构建系统是 ANT,在 Hudson 中运行
构建服务器是 Windows服务器
远程测试服务器是 Linux
不允许使用 rsync 守护进程,仅通过 ssh 进行 rsync
我们做了什么
将 DeltaCopy 安装到 Windows 服务器
按照这些说明设置 DeltaCopy,以便它可以与SSH
http://www.aboutmyip.com/AboutMyXApp/DisplayFAQ .do?fid=1
http://troy.jdmz.net/rsync/index.html
我构建了以下 ANT 宏,并在 hudson 中运行它。
起初,这个问题困扰着我,没有任何反应。
添加
-vvv
而不是-v
会产生不同的效果。 这导致更多信息被添加到 hudson 日志中。让它工作的关键是执行
C:\cygwin\bin\mintty.exe
而不是cmd
。然后,我在 Windows 服务器上手动运行 rsync 命令,其known_hosts 文件已使用远程主机进行更新。
这必须以 hudson 用户身份完成。 我们在 Windows 中使用 runas 命令以 hudson 用户身份启动 cygwin 控制台。
My experience with a similar issue:
My constants:
the build system was ANT, running in Hudson
the build server is Windows Server
the remote test server is Linux
not allowed to use rsync daemon only rsync over ssh
What we did
Installed DeltaCopy to the windows server
Followed these instructions to setup DeltaCopy so it will work with SSH
http://www.aboutmyip.com/AboutMyXApp/DisplayFAQ.do?fid=1
http://troy.jdmz.net/rsync/index.html
I built the following ANT macro, and ran it in hudson.
At first this hung on me, with no response.
Adding the
-vvv
rather than-v
made a different. This caused more info to be added to the hudson log.The key to getting this to work was to execute
C:\cygwin\bin\mintty.exe
rather thancmd
.I then manually ran the rsync command on the windows server its known_hosts file is updated with the remote host.
This had to be done as the hudson user. We used the runas command in windows to launch a cygwin console as the hudson user.
我最近实现了类似的东西,并认为我会分享我的经验,因为调试问题花了相当长的时间。 这个问题是谷歌上这个问题的唯一实例之一。
我的应用程序正在作为服务启动。 运行whoami命令时,结果为“ntauthority\system”。 在 /home/myusername/.ssh 下设置 SSH 密钥后,rsync 命令失败,因为 /home/SYSTEM/.ssh 下未设置任何配置。
将我的 /home/myusername/.ssh 目录复制到 /home/SYSTEM/.ssh 并使用以下字符串数组可以满足我的目的。 将 PORT 替换为您的 ssh 端口。
有关详细信息,请参阅此处的链接如果这是您的问题,请参阅以系统权限启动 cmd.exe 以进行测试。
I recently implemented something similar and thought I would share my experience, as it took quite some time to debug the problem. This question is one of the only instances of this problem on google.
My application was being launched as a service. When running the whoami command, the result was "nt authority\system". Having setup my SSH keys under /home/myusername/.ssh, the rsync command was failing as no configuration was set under /home/SYSTEM/.ssh.
Copying my /home/myusername/.ssh directory to /home/SYSTEM/.ssh and using the following String array worked for my purposes. Replace PORT with your ssh port.
See the link here for details about launching cmd.exe under system privileges for testing purposes if this is your issue.