LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-08-2008, 09:34 AM   #1
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 43
help with script to check processes on multiple servers


I found this script that tinkster posted in another thread that was similar to what I was looking for. Thanks tinkster!

for i in `cat hostlist`; do ssh root@$i -C ps -ef | grep mysqld | grep -v grep; done;


I modified it a bit to do what I need it to do but I have 2 questions.

1 How can I print the server name with the results, so I know if the process is running or not running on a specific server?

2 how can I add to this script a range of IPs instead of calling it from a hostlist file? ie. I want to check all servers in a range of 192.168.1.1 to 192.168.1.66


for the 2nd question I tried:
for i in (( $i=50 ; $i<=66 ; $i++ )); do ssh root@192.168.1.$i -C ps -ef | grep mysqld | grep -v grep; done;

got this error:

-bash: syntax error near unexpected token `('

thanks!

Last edited by ncsuapex; 05-08-2008 at 09:39 AM. Reason: .
 
Old 05-08-2008, 10:05 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,015
Blog Entries: 5

Rep: Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787
Code:
CNT=1
until [ $CNT -eq 67 ]
do IP=192.168.1.$CNT
   REMHOST=`ssh root@$IP -C hostname`
   echo "Host $REMHOST at IP $IP returned the following result:"
   ssh root@$IP -C ps -ef | grep mysqld | grep -v grep
   CNT=`expr $CNT + 1`
done
The above tells it to loop through from 1 to 66 (67 is where it ends).
It puts the hostname and IP address on the line above the ps output.
 
Old 05-08-2008, 10:12 AM   #3
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43
BEAUTIFUL! Worked perfectly. Thanks!
 
Old 05-08-2008, 10:13 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,470
Blog Entries: 54

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Code:
for ((i=50;i<=66;i++)) ; do
 echo -en "host 192.168.1.${i}: "
 ssh root@192.168.1.$i 'pgrep -f mysqld >/dev/null 2>&1&& echo YES||echo NO'
done
The above tells it to loop through from 50 to 66.
Somewhat like the previous poster did, this puts (or should) the IP address on the line *with* the output.
And unlike the previous poster this doesn't use ye aulde convoluted 'ps|grep|grep -v grep' but 'pgrep' (GASP! OMG!111!!).
 
Old 05-08-2008, 10:23 AM   #5
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43
^hmm... Only thing that didn't work with yours is it printed YES to all the servers, when I know a handful do not have mysqlsd running on them. When I logged into one of the servers that I know doesn't have mysqld running on it and just ran "pgrep -f mysqld >/dev/null 2>&1&& echo YES||echo NO" It printed "NO"




So I changed it to this: and it printed YES to the servers to does have mysqld running on it and no to the ones that didn't.

for ((i=50;i<=66;i++)) ; do
echo -en "host 192.168.1.${i}: "
ssh root@192.168.1.$i 'ps -ef | grep mysqld | grep -v grep >/dev/null 2>&1&& echo YES||echo NO'
done




thanks for the help!
 
Old 05-08-2008, 12:21 PM   #6
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,015
Blog Entries: 5

Rep: Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787
Quote:
Originally Posted by unSpawn View Post
Code:
for ((i=50;i<=66;i++)) ; do
 echo -en "host 192.168.1.${i}: "
 ssh root@192.168.1.$i 'pgrep -f mysqld >/dev/null 2>&1&& echo YES||echo NO'
done
The above tells it to loop through from 50 to 66.
Somewhat like the previous poster did, this puts (or should) the IP address on the line *with* the output.
And unlike the previous poster this doesn't use ye aulde convoluted 'ps|grep|grep -v grep' but 'pgrep' (GASP! OMG!111!!).
Previous poster didn't suggest the ps |grep ... - he incorporated it from what OP wrote.
 
Old 06-09-2008, 02:49 PM   #7
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43
ok new twist. Now I want to be able to copy a file from one machine(server65) to the rest of the machines on the network. Running the script from server65.


I modified the script to have a scp command and I added the authorized_keys from my host machine server65 to server3-8. If I ssh from server65 to server3-8 I don't get prompted for password, so the ssh keys work. But if I try ssh from server3-8 to server65 it prompts for PW. I do not have the keys set to go that way.


My question is.. In order to copy a file from server65 to server3-8 what do I put in for the command? If I manually do a scp filename server03:/location from server65 it works.


CNT=3
until [ $CNT -eq 9 ]
do IP=10.1.1.$CNT
REMHOST=`ssh root@$IP -C hostname`
echo "Host $REMHOST at IP $IP returned the following result:"
ssh root@$IP -C scp root@lifenet65:/location/file $IP:/location/file
CNT=`expr $CNT + 1`
done




nevermind:

changed it to this and it worked. It ALWAYS happens. I beat my head against the wall trying to figure something out, then I give in and post a message and within 5 minutes I figure it out myself



CNT=3
until [ $CNT -eq 9 ]
do IP=10.1.1.$CNT
echo "Host at IP $IP returned the following result:"
scp /location/file $IP:/location/file
CNT=`expr $CNT + 1`
done

Last edited by ncsuapex; 06-09-2008 at 02:56 PM. Reason: .
 
Old 06-10-2008, 11:02 AM   #8
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43
ok got a new one for you guys since I figured out that last one on my own.

I got this command which works as needed BUT 2 things

1 How do I print the hostname BEFORE the results? As it is now it prints the results then the hostname, which is fine, but it would look better and be easier to read if it printed the hostname on the line above the results

2 how can I get it to NOT print the hostnames that do not match the query? As the command runs now it prints all the hostnames from the hostname file. I only want to print the hostnames with results.


command.

for i in `cat hostnames`; do ssh user@$i -C /command | grep something ; echo "$i" ; done;




ok made a script out of the command but it still returns the hostname after the results

for i in `cat hostnames`
do ssh user@$i -C /command | grep something
REMHOST=$i
echo "Host $REMHOST at IP $IP returned the following result:"
done





edit:


once again I think i figured it out myself.

for i in `cat hostnames`
do
echo "hostname $i returned the following results:"
ssh user@$i -C /command | grep something
done;



does that look like it would work? Ive tested it but not enough to know for sure.

Last edited by ncsuapex; 06-10-2008 at 12:41 PM. Reason: .
 
  


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
Script needed to check then restart application processes damoj Linux - Newbie 2 04-15-2008 02:04 AM
Setting name servers on multiple servers centosfan Linux - Server 3 01-23-2008 03:43 PM
Multiple Blade servers booting from multiple disk drives simultaneously NGC_cheryl Linux - Enterprise 0 11-26-2007 08:38 AM
can you do threading or multiple processes in a shell script? BrianK Programming 8 08-07-2006 04:40 PM
Multiple threads vs Multiple processes deveraux83 Programming 1 05-11-2005 10:22 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration