This seems to be a pretty popular question, and one that has about 1,000 different answers depending on what forum you look at. Unfortunately, none work for me.
I’m trying to write a bash shell script that SSHes into a list of servers and runs a simple stop <x> service command to shutdown application servers on each machine.
To do this manually:
ssh user@server01.ourdomain.tld
user@server01.ourdomain.tld's password: ourpassword
Last login: Fri Nov 30 14:37:51 2012 from <some-ip-addr>
server01:[user@machinename ~]# (now we are SSHed in)
So I ask: given a set name of servers (server01 through server25), how can I write a bash script to SSH into all of them and run service ourservice stop? This script should not require any human interaction once kicked off, and so should provide the SSH command with the appropriate password (ourpassword) to use. Furthermore, I need properly exit (or just close connections) after the script SSHes into each server so we don’t hang resources (open connections, etc.). Thanks in advance.
Do this
ssh-keygencommand.ssh/authorized_keysfolderNow you can connect to any server without typing a password, which is our first objective here.
Now you can send commands to your servers using the following syntax:
There’s a nice if short description of this at this page.
So, you just concatenate your 25 commands in a file and fire that up, like this: