简体   繁体   English

远程执行-同一行有2个命令输出

[英]Remote execution - 2 command outputs in same line

shell#printf " hostname rpm -qa | grep rhncfg-actions \\n" usmnxbox01 rhncfg-actions-5.10.85-1.el6.noarch shell#printf“ hostname rpm -qa | grep rhncfg-actions \\ n” usmnxbox01 rhncfg-actions-5.10.85-1.el6.noarch

I am trying the get the above output using ssh from multiple hosts using the command below. 我正在尝试使用下面的命令从多个主机使用ssh获取以上输出。

for i in `cat ahosts`; do ssh -t $i "printf "`hostname` `rpm -qa | grep rhncfg-actions`\n"" 2>/dev/null;done

but I get the hostname of the system am I running this command on. 但是我在运行此命令时得到系统的主机名。

You need to put the command substitutions in single quotes to prevent them from being evaluated locally before being sent to the remote host. 您需要将命令替换项放在单引号中,以防止在将替换项发送到远程主机之前对其进行本地评估。 Your code also has several other problems: 您的代码还存在其他一些问题:

  1. Using a for loop to iterate over a file 使用for循环遍历文件
  2. Using command substitutions in the format string argument to printf (which causes problems if any of the substitutions contain a percent sign). printf的format字符串参数中使用命令替换(如果任何替换包含百分号,都会引起问题)。

Use this instead: 使用此代替:

while IFS= read -r i; do
    ssh -t "$i" 'printf "%s %s\n" "$(hostname)" "$(rpm -qa | grep rhncfg-actions)"' < /dev/null
done < ahosts

This redirects the standard input for ssh from /dev/null so that ssh doesn't incorrectly consume input meant for the read command. 这将从/dev/null重定向ssh的标准输入,以便ssh不会错误地消耗用于read命令的输入。

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

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