Script to get the report of the process running in 30 Servers (660 Views)
Reply
Occasional Advisor
Kadavan
Posts: 16
Registered: ‎01-31-2011
Message 1 of 14 (660 Views)
Accepted Solution

Script to get the report of the process running in 30 Servers

Hi All

I need you valuable belp in creating a script that will login 30 servers and create a report of the process run by users other that root.

Regards
-Binu
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Hi:

What have you tried?

I suggest using 'ssh -n ' for example to login to a list of servers and return information.

Something driven like:

while read HOST
do
ssh -n ${HOST} ps -ef
done < /home/binu/hostnames

Regards!

...JRF...
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 3 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Kadavan,

for this you will need at least the following:

- a file containing the name of the servers
- ssh keyed access (you don't want to type all the passwords per hand, do you?)

anyway, a while loop would do this, see above...
****
Unix operates with beer.
Occasional Advisor
Kadavan
Posts: 16
Registered: ‎01-31-2011
Message 4 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Hi Victor

Where can I mention the host file details in the the script

Regards
-Binu
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 5 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

> Where can I mention the host file details in the the script


it is in the last line, where you specify that file. in our case it's /home/binu/hostnames :

#

while read HOST
do
ssh -n ${HOST} ps -ef
done < /home/binu/hostnames

#
****
Unix operates with beer.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Hi (again):

> Where can I mention the host file details in the the script

The file that I showed in my example --- '/home/binu/hostnames' simply consists of a server's hostname, one name per line of the file.

Regards!

...JRF...
Occasional Advisor
Kadavan
Posts: 16
Registered: ‎01-31-2011
Message 7 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Hi Victor

Where will be the output of ps -ef get stored ..Is it possible to store the output of ps -ef of all servers in a single file.

Regards
-Binu
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 8 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

> Where will be the output of ps -ef get stored ..Is it possible to store the output of ps -ef of all servers in a single file.

yes, use redirection:

while read HOST
do
ssh -n ${HOST} ps -ef
done < /home/binu/hostnames 1> /home/binu/output 2>&1
****
Unix operates with beer.
Occasional Advisor
Kadavan
Posts: 16
Registered: ‎01-31-2011
Message 9 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Thanks Viktor and James .The script is working .It is asking for password for each host ..Can we do some thih for this

and Is there way to arrange the output file according to the hostname

Regards
-Binu
Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 10 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Look at Victor's first answer.. You need authorized key pairs between the master and client servers.

If you don't know how to do this, I personally described this process at least in 10 different posts. Look at my answer history and find one of those. It is not hard but it needs precise execution for file ownerships and permissions.

Or search google for creating shh keys for passwordless login and you will find a tgon of documents explaining how to do this.
________________________________
UNIX because I majored in cryptology...
Occasional Advisor
Kadavan
Posts: 16
Registered: ‎01-31-2011
Message 11 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

Thank you all your help ..I was able to do completed the script as foollows

while read HOST
do
ssh -n ${HOST} hostname
ssh -n ${HOST} ps aux | grep -v root

done < /home/binu/scripts/hostnames 1> /home/binu/scripts/output 2>&

Thanks
-Binu
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 12 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

> ssh -n ${HOST} hostname

What does that tell you that you didn't
already know?
Occasional Advisor
Kadavan
Posts: 16
Registered: ‎01-31-2011
Message 13 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

This entry made to update the output file with hostname and then the Process details ..Other wise we cannot make out which process is from which server

Looks like this
cpaiqp4a

USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
tauser02 1396842 0.9 9.0 1385060 1385096 - A Mar 22 824:12 /opt/isv/WAS51
tauser02 3653858 0.4 9.0 1380776 1380812 - A Mar 22 350:33 /opt/isv/WAS51
www 4370646 0.1 2.0 283820 283588 - A Feb 28 243:21 /opt/isv/IBMIH
tauser02 4788304 0.0 1.0 136920 136956 - A Feb 28 55:44 /opt/isv/WAS51
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 14 of 14 (660 Views)

Re: Script to get the report of the process running in 30 Servers

> This entry made to update the output file
> with hostname [...]

Something simple (and local) might be faster:

echo ${HOST}

You're giving the name of the remote host to
"ssh", so that you can ask the remote host to
tell you its name? (I can imagine cases
where you might get a different answer back,
but none seems very likely.)

If you really want the name from the remote
host, do you need two "ssh" sessions?

> ssh -n ${HOST} ps aux | grep -v root

That does the "ps" on the other system, but
it does the "grep" locally, doesn't it? I
haven't tested it, but I'd try to do all the
work on the remote system:

ssh -n ${HOST} 'ps aux | grep -v root'

And, if that worked, then I'd try something
like:
[...] 'hostname ; ps aux | grep [...]'
in _one_ "ssh" command.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.