linux - ssh 管道命令错误

标签 linux ssh openssh

我在一台机器上运行

ssh root@lPServerHgl  cat /root/devops/giveAllArtifacts.txt | xargs -I {} sh -c "grep {} /ci/test.log;"

我收到 grep:/ci/test.log: No such file or directory

但在服务器上使用相同的命令(cat/root/devops/giveAllArtifacts.txt | xargs -I {} sh -c "grep {}/ci/test.log;"): lPServerHgl 正常工作,没有任何错误。

P.S ssh 连接没有任何问题。所有其他命令都在 ssh 上运行。管道命令似乎有一些问题。

最佳答案

管道在您的本地计算机上运行,​​而不是在远程服务器上运行。

ssh user@host remote-cmd [...args] | local-cmd [...args]
--------- runs in server --------- | --- runs locally ----

管道由您的 shell 解释,而不是远程的。要在 ssh 连接的另一端执行管道,您必须在那里调用一个 shell。例如:

ssh user@host bash -c "'cat file.txt | grep foo'"

注意内引号。这会将管道发送到另一台机器上的 bash 进程。

输入这些命令可能很麻烦且容易出错。您可能希望在远程计算机上放置一个随时可以运行的脚本,以便使用单个命令调用它。

关于linux - ssh 管道命令错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42074384/

相关文章:

linux - 隧道 SSH : access a server blocked by firewall through another server

git - 如何在 Windows 上将现有 key 添加到 openssh?

linux - 我们如何在使用循环时调用 ansible playbook 中的变量

PHP 警告 : Unable to load dynamic library '/usr/lib64/php/modules/solr.so' undefined symbol: php_json_decode_ex in Unknown on line 0

linux - 使用 sshpass 从本地 ubuntu 系统在远程服务器上执行脚本时,如何从远程 Ubuntu 服务器获取环境变量?

linux - 使用 BASH 在不同的盒子上启动多个进程

linux - 转发 X 连接时,OpenSSH 客户端在注销时挂起

ssh/配置 : line 1: Bad configuration option: include

linux - 在 linux 中添加到 $PATH 以便守护进程可用

linux - 我如何根据测试 .gz 的结果执行指令? Linux操作系统