I've searched around a bit for similar questions, but other than running one command or perhaps a few command with items such as:
ssh user@host -t sudo su -
However, what if I essentially need to run a script on (let's say) 15 servers at once. Is this doable in bash? In a perfect world I need to avoid installing applications if at all possible to pull this off. For argument's sake, let's just say that I need to do the following across 10 hosts:
- Deploy a new Tomcat container
- Deploy an application in the container, and configure it
- Configure an Apache vhost
- Reload Apache
I have a script that does all of that, but it relies on me logging into all the servers, pulling a script down from a repo, and then running it. If this isn't doable in bash, what alternatives do you suggest? Do I need a bigger hammer, such as Perl (Python might be preferred since I can guarantee Python is on all boxes in a RHEL environment thanks to yum/up2date)? If anyone can point to me to any useful information it'd be greatly appreciated, especially if it's doable in bash. I'll settle for Perl or Python, but I just don't know those as well (working on that). Thanks!
To give you the structure, without actual code.
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 useperl
likessh 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 <<\EOF
define stuff like
TOMCAT_WEBAPPS=/usr/local/share/tomcat/webapps
tar xj the_package.tbz
orrsync rsync://repository/the_package_place
mv $TOMCAT_WEBAPPS/old_war $TOMCAT_WEBAPPS/old_war.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
Just read a new blog using setsid without any further installation/configuration besides the mainstream kernel. Tested/Verified under Ubuntu14.04.
While the author has a very clear explanation and sample code as well, here's the magic part for a quick glance:
Have you looked at things like Puppet or Cfengine. They can do what you want and probably much more.
I'm not sure if this method will work for everything that you want, but you can try something like this:
Which will run the script (which resides locally) on the remote server.
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.