0

I want to print disk space percentage, path and host for 100+ machines. I have created below script to do that. But it's just working for the first host not for the rest of all in the host list. I slimmed down the possibility of error is with executing raw SSH command. Pls suggest any alternative. Thanks.

#!/bin/bash
set -x
echo "starting..."

begin ()
{
threshold=60
while read host
 do
  finder
 done <hosts.txt
}

finder ()
{
ssh ops@$host 'df -h' | grep -o '[0-9]\+%'| grep -o '[0-9]\+'| while read percent
do
  if [ $percent -gt $threshold  ]
   then
   path=`ssh ops@$host 'df -h' | tr -s " " | grep -oE "$percent% /[a-z]\+|$percent% /" | cut -d " " -f2`
   echo "Alert! Time to wipe off! It is $percent% in \"$path\" at $host"
  else
   echo "Info: It is $percent in $path at $host"
 fi
done 
}

begin

It gives me result like this: Alert! Time to wipe off! It is 61% in "/" at 192.168.8.31

my hosts.txt looks like below:

192.168.8.31
192.168.7.41
192.168.7.42
192.168.8.32

So, in the script I'm executing df -h in each host via ssh and for each line of the df -h command I'm checking with the threshold value. If the condition is true alerting the guys, if not, just informing. Pls help me to understand what went wrong ? thanks :)

dm90
  • 775
  • 1
  • 10
  • 24
  • 1
    Your `ssh` processes are draining your standard input. You need to redirect stdin on them or use the `-n` flag to avoid that. (Not adding this as an answer as this is a duplicate I just can't find the others at the moment.) – Etan Reisner Sep 17 '15 at 12:30
  • The `-n` flag did the magic for me :) thanks ! – dm90 Sep 18 '15 at 05:18

0 Answers0