简体   繁体   中英

ssh remote command execution quoting and piping awk

I'm working on a script, that should find certain disks and add hostname to them.

I'm using this for 40 servers with a for loop in bash

#!/bin/bash

for i in myservers{1..40}
    do ssh user@$i findmnt -o SIZE,TARGET -n -l |
            grep '1.8T\|1.6T\|1.7T' | 
            sed 's/^[ \t]*//' |
            cut -d ' ' -f 2 |
            awk -v HOSTNAME=$HOSTNAME '{print HOSTNAME ":" $0}'; done | 
    tee sorted.log

can you help out with the quoting here? It looks like awk gets piped (hostname) from localhost, not the remote server.

Everything after the first pipe is running locally, not on the remote server.

Try quoting the entire pipeline to have it run on the remote server:

#!/bin/bash

for i in myservers{1..40}
    do ssh user@$i "findmnt -o SIZE,TARGET -n -l | 
        sed 's/^[ \t]*//' |
        cut -d ' ' -f 2 |
        awk -v HOSTNAME=\$HOSTNAME '{print HOSTNAME \":\" \$0}'" ;
done | tee sorted.log

This is a shorter version of your stuff:

findmnt -o SIZE,TARGET -n -l | 
awk -v HOSTNAME=$HOSTNAME '/M/{print HOSTNAME ":" $2}'

Applied to the above:

for i in myservers{1..40}
    do ssh user@$i   bash -c ' 
            findmnt -o SIZE,TARGET -n -l | 
              awk -v HOSTNAME=$HOSTNAME '"'"'/M/{print HOSTNAME ":" $2}'"'"' '
         done | 
    tee sorted.log

see: How to escape the single quote character in an ssh / remote bash 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