简体   繁体   中英

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

I am trying the get the above output using ssh from multiple hosts using the command below.

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
  2. Using command substitutions in the format string argument to printf (which causes problems if any of the substitutions contain a percent sign).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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