LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run shell script on multiple linux servers (https://www.linuxquestions.org/questions/linux-newbie-8/run-shell-script-on-multiple-linux-servers-945046/)

vanish78 05-15-2012 06:13 AM

Run shell script on multiple linux servers
 
Hi There,
RHEL

I want a script that will check disk space on multiple servers. I can do a df -h with :

PHP Code:

for i in server1 server2 server3
do
ssh $i df -h
done 

But I want to run this script (which this forum helped me with!)

PHP Code:

#!/bin/sh
for i in msrac01
do
ssh $i df=`df -Pl | grep "^/dev" | awk '{print $5, $6}' | sed "s/%//"`
echo 
"$df| while read percent fs
do
    if [ 
$percent -gt 90 ] ; then
       
echo "$percent"
    
fi
done 

Please help.

pan64 05-15-2012 06:32 AM

I remember I saw this script, and I also remember I gave you a one liner instead of this terrible code.
So I suggest you to use a much simple script and you will be able to use that with ssh:

http://www.linuxquestions.org/questi...9/#post4659230

Code:

cmd="this is a one line long script"
for i in server1 server2 server3
do
ssh $i $cmd
done

and also you may need take care of ' and " and ` signs.

colucix 05-15-2012 07:09 AM

I agree with pan64. Using a simple command is far simpler. In addition, to avoid the quotes headache you might run the df command remotely and parse it locally, e.g.
Code:

#!/bin/sh
for i in something
do
  df=`ssh $i "df -Pl"`
  echo "$df" | awk '/^\/dev/{sub(/%/,"",$5); if ($5 > 90) print $5, $6}'
done

adding something to identify the server from which you get some result.

Moreover, notice that usually you don't need to run grep, awk and sed together, since awk can do it all! :jawa:

vanish78 05-16-2012 09:39 AM

Hi,

Ok I used the "lighter script" :

PHP Code:

#!/bin/sh
for i in server1
do
  
df=`ssh $i "df -Pl"`
  echo 
"$dfawk '/^\/dev/{sub(/%/,"",$5); if ($5 > 95) print $5}'
done 

But not getting any results, but it works on server2. What can be the reason?

colucix 05-16-2012 09:48 AM

No filesystem with usage > 95%? What is the difference between the two servers in terms of OS, shell, awk version and so on?

pan64 05-16-2012 10:42 AM

I would simplify it:
Code:

for i in <server list>
do
  echo server: $i  # if you want to print it just do it
  ssh $i df -Pl | awk ' here is the script '
done

you can also try awk -v server="$i" ' script ' if you want to handle the server name inside the awk script.


All times are GMT -5. The time now is 03:07 PM.