Operating System - HP-UX
1833796 Members
4662 Online
110063 Solutions
New Discussion

Searching a word pattern on many servers from a particular server

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Searching a word pattern on many servers from a particular server

Hello,

I am trying to search a particular word starting from current directory
as below:

$find . -type f -exec grep -i "shiv" /dev/null {} \;

The above command works fine on a single server.

Now i want to search a particular word pattern starting with some directory and traversing its sub-directories
on number of servers. My userid is ssh enabled to all the required servers.

Can someone tell how to do this ?

Thanks,
Shiv
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Searching a word pattern on many servers from a particular server

Hi SHiv:

You can do:

for HOST in server1 server2 server3
do
ssh ${HOST} 'find /path -type f -exec grep -i "shiv" /dev/null {} +'
done

...or if the file "HOSTLIST" has the server names (one per line):

while read HOST X
do
ssh ${HOST} 'find /path -type f -exec grep -i "shiv" /dev/null {} +'
done < HOSTLIST

...

Notice that I used the "+' terminator which optimizes things by bundling many arguments (Here, file names) into a list and spawning one 'grep' process to handle them.

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: Searching a word pattern on many servers from a particular server

Mr. Ferguson's scripts have hit the nail on the head.

The only change I would make would be to add some echo statements to Mr. Ferguson's scripts so that you know which server you are running on and to provide some spacing between results.

for HOST in server1 server2 server3
do
echo ${HOST}
ssh ${HOST} 'find /path -type f -exec grep -i "shiv" /dev/null {} +'
echo ""
done

...or if the file "HOSTLIST" has the server names (one per line):

while read HOST X
do
echo ${HOST}
ssh ${HOST} 'find /path -type f -exec grep -i "shiv" /dev/null {} +'
echo ""
done < HOSTLIST
Patrick Wallek
Honored Contributor

Re: Searching a word pattern on many servers from a particular server

P.S. No offense intended Jim!
James R. Ferguson
Acclaimed Contributor

Re: Searching a word pattern on many servers from a particular server

Hi (again) Shiv:

Yes, my friend Patrick makes a very good suggestion! Most certainly it would be nice to add the hostname to the output!

/* no points for this comment */

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Searching a word pattern on many servers from a particular server

One other thing you can do with JRF's script is to run it in parallel and then aggregate the results from the separate output files:

for HOST in $(< HOSTLIST); do
ssh ${HOST} 'find /path -type f -exec grep -i "shiv" /dev/null {} +' > $HOST.out &
done
wait

for HOST in $(< HOSTLIST); do
echo "Searching $HOST:"
cat $HOST.out
done