LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-15-2012, 07:13 AM   #1
vanish78
LQ Newbie
 
Registered: May 2011
Posts: 13

Rep: Reputation: 9
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.
 
Old 05-15-2012, 07:32 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,512

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
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.
 
1 members found this post helpful.
Old 05-15-2012, 08:09 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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!
 
1 members found this post helpful.
Old 05-16-2012, 10:39 AM   #4
vanish78
LQ Newbie
 
Registered: May 2011
Posts: 13

Original Poster
Rep: Reputation: 9
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?
 
Old 05-16-2012, 10:48 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
No filesystem with usage > 95%? What is the difference between the two servers in terms of OS, shell, awk version and so on?
 
Old 05-16-2012, 11:42 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,512

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - Change user & run multiple commands ijf99 Linux - Newbie 1 01-26-2009 07:15 AM
What is the easiest way to run a shell script in the background on 10 servers? abefroman Linux - Software 3 03-12-2008 10:24 AM
How to run multiple X servers. samkraju Red Hat 1 12-30-2007 12:07 PM
Can't run multiple X servers jojotx0 Linux - Software 3 08-12-2006 11:31 AM
how to run a shell script in Linux chandhru Linux - Newbie 2 09-27-2002 02:19 PM


All times are GMT -5. The time now is 10:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration