简体   繁体   中英

Bash + SSH + Grep not generating output

I'm trying to use the script below to extract values from the df command on remote servers, then record to a log file. SSH keys are in place and no password is needed (this is not the problem).

It's getting hung up, however, and not spitting back output.

#!/bin/bash

PATH=/bin:/usr/bin:/usr/sbin
export PATH

SERVERLIST=/opt/scripts/server-list.dat

while IFS='|' read -u 3 hostname; do

echo evaluating $hostname...

SIZE=$(ssh $hostname | df -Pkhl | grep '/Volumes/UserStorage$' | awk '{print $2}')
 echo $SIZE


done 3< $SERVERLIST

exit 0

You need to run df on the remote system, not pipe the output of an interactive ssh to it:

SIZE=$(ssh -n $hostname df -Pkhl | grep '/Volumes/UserStorage$' | awk '{print $2}')

Also, use the -n option to ssh to keep it from trying to read from stdin , which would consume the rest of the lines from the server list file.

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