How can I automate running commands remotely over SSH?

Often, I'll just use the original Tcl version of Expect. You only need to have that on the local machine. If I'm inside a program using Perl, I do this with Net::SSH::Expect Other languages have similar "expect" tools.

Often, I'll just use the original Tcl version of Expect. You only need to have that on the local machine. If I'm inside a program using Perl, I do this with Net::SSH::Expect.

Other languages have similar "expect" tools.

2 Went this route. Thanks for the tip (and the edit). Other had good ideas, but I'm looking to run a some 300 line script.

Perl brings a bit of 'elegance' to that that bash won't provide. (Perl.. elegance.. never thought I'd say that) :) – f4nt Nov 2 '08 at 16:47 1 If you do perl right, it can be nice ;) – Johan Nov 9 '09 at 9:02.

The issue of how to run commands on many servers at once came up on a Perl mailing list the other day and I'll give the same recommendation I gave there, which is to use gsh: outflux.net/unix/software/gsh gsh is similar to the "for box in box1_name box2_name box3_name" solution already given but I find gsh to be more convenient. You set up a /etc/ghosts file containing your servers in groups such as web, db, RHEL4, x82.6.7, or whatever (man ghosts) then you use that group when you call gsh. Pdurbin@beamish ~$ gsh web "cat /etc/redhat-release; uname -r" www-2.foo.Com:" rel="nofollow">www-2.foo.Com: Red Hat Enterprise Linux AS release 4 (Nahant Update 7) www-2.foo.

Com: 2.6. 2.6.8.0.1. ELsmp www-3.foo.Com: Red Hat Enterprise Linux AS release 4 (Nahant Update 7) www-3.foo. Com: 2.6. 2.6.8.0.1.

ELsmp www-4.foo.Com: Red Hat Enterprise Linux Server release 5.2 (Tikanga) www-4.foo. Com: 2.1.138-92.1.13. El5 www-5.foo.Com: Red Hat Enterprise Linux Server release 5.2 (Tikanga) www-5.foo.

Com: 2.1.138-92.1.13. El5 pdurbin@beamish ~$ You can also combine or split ghost groups, using web+db or web-RHEL4, for example. I'll also mention that while I have never used shmux, its website contains a list of software (including gsh) that lets you run commands on many servers at once.

Capistrano has already been mentioned and (from what I understand) could be on that list as well.

Take a look at Expect (man expect) I've accomplished similar tasks in the past using Expect.

You can run a local script as shown by che and Yang, and/or you can use a Here document: ssh root@server /bin/sh.

To give you the structure, without actual code. Use scp to copy your install/setup script to the target box. Use ssh to invoke your script on the remote box.

That might have to work in the end. I was hoping for something a bit prettier than that. In the end, that really only cuts out the pulling down of the script from subversion, and exporting a bash script is a minimal loss of time.

– f4nt Oct 28 '08 at 15:37 You can use a single script to do all that automatically. Without having you to type all the commands, in case that wasn't clear, f4nt – Vinko Vrsalovic Oct 28 '08 at 20:28.

You can try paramiko. It's a pure-python ssh client. You can program your ssh sessions.

Nothing to install on remote machines. See this great article on how to use it.

Although it's a complex topic, I can highly recommend Capistrano.

You can do it the same way you did before, just script it instead of doing it manually. The following code remotes to machine named 'loca' and runs two commands there. What you need to do is simply insert commands you want to run there.

Che@ovecka ~ $ ssh loca 'uname -a; echo something_else' Linux loca 2.6.25.9 #1 (blahblahblah) something_else Then, to iterate through all the machines, do something like: for box in box1_name box2_name box3_name do ssh $box 'commmands_to_run_everywhere' done In order to make this ssh thing work without entering passwords all the time, you'll need to set up key authentication. You can read about it at IBM developerworks.

You can run the same command on several servers at once with a tool like cluster ssh. The link is to a discussion of cluster ssh on the Debian package of the day blog.

Well, for step 1 and 2 isn't there a tomcat manager web interface; you could script that with curl or zsh with the libwww plug in. For SSH you're looking to: 1) not get prompted for a password (use keys) 2) pass the command(s) on SSH's commandline, this is similar to rsh in a trusted network. Other posts have shown you what to do, and I'd probably use sh too but I'd be tempted to use perl like ssh tomcatuser@server perl -e 'do-everything-on-one-line;' or you could do this: either scp the_package.

Tbz tomcatuser@server:the_place/. Ssh tomcatuser@server /bin/sh Old mv $THE_PLACE/new_war $TOMCAT_WEBAPPS/new_war touch $TOMCAT_WEBAPPS/new_war you don't normally have to restart tomcat mv $THE_PLACE/vhost_file $APACHE_VHOST_DIR/vhost_file $APACHECTL restart might need to login as apache user to move that file and restart EOF.

You want DSH or distributed shell, which is used in clusters a lot. Here is the link: dsh You basically have node groups (a file with lists of nodes in them) and you specify which node group you wish to run commands on then you would use dsh, like you would ssh to run commands on them. Dsh -a /path/to/some/command/or/script It will run the command on all the machines at the same time and return the output prefixed with the hostname.

The command or script has to be present on the system, so a shared NFS directory can be useful for these sorts of things.

Have you looked at things like Puppet or Cfengine. They can do what you want and probably much more.

Here's a script to do an SSH command on multiple machines, it may help: #! /bin/bash MACHINES="machine1 machine2 machine3 machine4 machine5 machine6" for m in $MACHINES do cmd="ssh $m '$*'" eval $cmd done You could even put this so it's in your path, call it something like sshAll, then just type sshAll command.

You can pipe the local script to the remote server and execute it with one command: ssh -t user@host 'sh.

I'm not sure if this method will work for everything that you want, but you can try something like this: $ cat your_script. Sh | ssh your_host bash Which will run the script (which resides locally) on the remote server.

Pssh may be interesting since, unlike most solutions mentioned here, the commands are run in parallel. (For my own use, I wrote a simpler small script very similar to GavinCattell's one, it is documented here - in french).

Multixterm is cool for doing this, and allows you to easily put the 1 out of n machine back in sequence. freshmeat.net/projects/multixterm.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions