0

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.

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

1 Answers1

2

Do this

  1. Make yourself a public/private keypair, using the ssh-keygen command
  2. Save your private key in all of your servers, inside the .ssh/authorized_keys folder

Now 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:

ssh username@serverid 'command'

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:

ssh user@server1.yourservers.com 'service ourservice stop'
ssh user@server2.yourservers.com 'service ourservice stop'
misterakko
  • 123
  • 6
  • Thanks @misterakko (+1) - if I actually want to run a shell script on each server, can you just confirm that the command would be: `ssh root@server1.yourservers.com './run-myscript.sh'`? Thanks again! – IAmYourFaja Dec 02 '12 at 15:45
  • 1
    It's not a good idea to allow root login from ssh! This is fortunately turned off by default on sensible ssh servers. – gniourf_gniourf Dec 02 '12 at 15:56
  • @gniourf_gniourf Security was not a factor in the original question. Personally, I close the SSH port on my firewalls for any IP except the fixed one from my offices, so I have no fear in ssh'ing as root. As always, security means inconveniencing work, so you choose the best compromise you find. – misterakko Dec 02 '12 at 16:18
  • The OP has `user` and not `root` as login. Now, you do whatever you want to do with your servers, but I'd prefer not to see a root login in an answer given to a novice user. – gniourf_gniourf Dec 02 '12 at 16:27
  • Anybody? Is that how I would run the script? – IAmYourFaja Dec 02 '12 at 17:00
  • @HeineyBehinds Yes, but be careful. Basically, what you're doing here is opening a shell on the remote machine just for the time it takes to run the command. If your script tries to fork or fire background commands - directly or indirectly - those processes might be terminated when the mother shell is killed in a very short amount of time. These conditions can be hard to understand and debug. – misterakko Dec 03 '12 at 04:47