Visit Our UNIX and Linux User Community


How to run same script on multiples servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run same script on multiples servers
# 1  
Old 07-21-2009
How to run same script on multiples servers

Hi All,
I have some script that run some commands and send results to my email. I want to run same script on mulitiple servers. How can I do that. I know there is an option "ssh". But I'm not quite sure how to use it in the script.

And also. scripts has some parameters like following,

if [ -f "/some/some/some1"]: then

do something.

"/some/some/some1" will vary by the server.

Any body have an idea. Thanks
# 2  
Old 07-21-2009
I recommend setting up an ssh key pair for passwordless logins on the machines you will be running the script on. For this to work you will need to put a copy of the script you are trying to run on each server. Then you can write a simple "launcher" script to ssh into each machine and run the script that are wanting to run on each machine. The launcher script doesn't need to be anything more than a simple for loop.
Step by step:

1. Set up the ssh keys on each machine. The ssh man page explains how to do it.

2. Put a copy of the script on each machine

3. Write a simple launcher script. It will look something like this
#!/bin/ksh

for server in host1 host2 host3
do
ssh $server /path/to/script
done

4. Enjoy your time saved
# 3  
Old 07-21-2009
Thanks for your reply.
I forgot to mention that, I get an email after I run the scripts, so if I set up ssh to run the scripts on multiple servers, can I put results from all servers to one file,then send an email instead of sending mail from each server. Thanks
# 4  
Old 07-21-2009
You could have the script running on each machine append the results to a text file on an nfs server and then have the launcher script email you the file as an attachment and mail it to you or read the file and mail the text to you.
# 5  
Old 07-21-2009
Thanks for your quick reply.

I can able to ssh to other servers and able run the command. I'm getting emails from each server. To aviod this, I'm trying to append file using ssh using following command

ssh user@some.domain "cat remote-source-file-name" >> local-target-file-name

since I have to add this command to the script for several servers and need to remove the file from all servers, the script will be too long.
if I can do it in oppisite direction using following command

cat local-source-file-name | ssh user@some.domain "cat >> remote-target-file-name"

i can send result file from all servers and append to the file where (server A) the intial command runs. from there I can send an email with result file. But for this, Do I need to copy RSA keys from all servers to server A. Because when I run the above command, it asking me for the password. But I can ssh from server A to all other servers without the password. Any one have idea. Thanks.
# 6  
Old 07-21-2009
Do you have any NFS servers in your environment? If you do just append the output of the script to the same file on an NFS share and then mail that one file to yourself.
# 7  
Old 07-21-2009
If you run an ssh command remotely the stdout from the command is returned to your local terminal. You can capture it there and send to a local file. So, say you want the output of the date command from multiple servers put in a file and then emailed. You could just:

Code:
#!/bin/bash
hosts=( host1 host2 host3 )
for ((i=0;i<${#hosts[@]};i++)); do
  ssh me@${hosts[$i]} "date" >> outputfile &
done
wait
mail -s "Results" me@domain.com < outputfile

if it's a script that has to run they you could have it in a shared location and run it

Code:
  ssh me@${hosts[$i]} "/path/to/script.sh" >> outputfile &

If there's something in the script that varies by server you're just going to have to work that out. Either have a different script in each location or, if you need more automation, have it accept command line input for the difference and pass it with the script:

Code:
#!/bin/bash
hosts=( host1 host2 host3 )
var=( var1 var2 var3 )
for ((i=0;i<${#hosts[@]};i++)); do
  ssh me@${hosts[$i]} "/path/to/script.sh ${var[$i]}" >> outputfile &
done
wait
mail -s "Results" me@domain.com < outputfile

(As you can see I'm an array junkie. Might be overkill here. Dunno. Works for me though Smilie If you need any parts of my example explained please don't hesitate to ask)

Previous Thread | Next Thread
Test Your Knowledge in Computers #716
Difficulty: Medium
The theory of computable functions by Alan Turning greatly influenced the construction of modern digital computers.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a shell script on all 15 servers at the same time?

We have 15 servers. Hostnames for these 15 servers are stored in a text files and loop through each server to connect to the remote server and run a command, but this loop process runs the command one after another. However, the requirement is to run the same command on all 15 servers at the same... (10 Replies)
Discussion started by: laknar
10 Replies

2. Shell Programming and Scripting

Run a script on multiple servers

I need to run a script on a bunch of remote servers. how can this be done without ssh into each individual server and run it its under /sbin/script.sh on each server (1 Reply)
Discussion started by: tdubb123
1 Replies

3. AIX

How to write a script to run without password on a batch of servers?

I need run a command such as ps -ef |grep xxx on a batch of servers, how to write a script to run it without password? don't need go in each server to check? Thanks (7 Replies)
Discussion started by: rainbow_bean
7 Replies

4. Shell Programming and Scripting

Run multiples jobs in background mod

Hi, I have a problem runnig from script two diferents process in background mod. Is there a limitation to do this? For example echo "Running process one:" ./ProcessOne & echo "Running process two:" ./ProcessTwo & echo "The process are runnig. Bye" The problem is that the... (7 Replies)
Discussion started by: Xedrox
7 Replies

5. Shell Programming and Scripting

Run a script in parts on 2 servers

Hi all, I have a task for which I need to run some commands on one server1 and then jump from the server1 (using ssh and asking user to enter login credentials manually for server2 server) to server2 and run some commands there and exit. I know the script I need here but problem is when I... (5 Replies)
Discussion started by: pat_pramod
5 Replies

6. Shell Programming and Scripting

run vi/vim encrypted shell script without decryption on multiple servers

Hello Everyone, How do we run vi/vim encrypted shell script without decryption on multiple servers. It is a simple bash script and vim -nx <filename> has been used to encrypt with desired password. Now I have few errors, the syntax is absolutely fine as I have run that script multiple times on... (0 Replies)
Discussion started by: lovesaikrishna
0 Replies

7. Shell Programming and Scripting

Need a script to run on multiple mail servers..

Hello, I am a Unix newbie and I need a script in which I can run a command on multiple servers at work. The command is to start a storage process and I am sick of doing it manually on all servers.. Here's the command: /opt/bss/bin/snmptable -CB -v2c -c P67LzuBm hostname hrStorageTable... (4 Replies)
Discussion started by: kinyyy
4 Replies

8. UNIX for Advanced & Expert Users

Command to run across servers

Hi, I have to run several unix commands like lsof or sed and need to execute across different servers as part of my monitoring tasks. There are around 40 Unix Servers. It is really cumbersome to run those command on several servers. can some help me in this regard. Is it possible to run... (6 Replies)
Discussion started by: john_prince
6 Replies

9. AIX

is it ok to run AIX mksysb script on vio servers?

Hi to all now we running mksysb to nim on aix servers. we are planning to take mksysb from vio servers. can we use the same script for vio servers. thanks (10 Replies)
Discussion started by: honeym210
10 Replies

10. Shell Programming and Scripting

ksh to run servers

I want to write a Kshell program which will start the servers(Oracle,DataIntegrator). Can anybody help me with this? I would appreciate your help. Thanks in advance (0 Replies)
Discussion started by: pari111222
0 Replies

Featured Tech Videos