How can I automate running commands remotely over

2019-01-21 08:21发布

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:

  1. Deploy a new Tomcat container
  2. Deploy an application in the container, and configure it
  3. Configure an Apache vhost
  4. 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!

21条回答
SAY GOODBYE
2楼-- · 2019-01-21 08:48

To give you the structure, without actual code.

  1. Use scp to copy your install/setup script to the target box.
  2. Use ssh to invoke your script on the remote box.
查看更多
看我几分像从前
3楼-- · 2019-01-21 08:49

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 <<\EOF
define stuff like TOMCAT_WEBAPPS=/usr/local/share/tomcat/webapps
tar xj the_package.tbz or rsync 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

查看更多
别忘想泡老子
4楼-- · 2019-01-21 08:49

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:

#----------------------------------------------------------------------
# Create a temp script to echo the SSH password, used by SSH_ASKPASS
#----------------------------------------------------------------------
SSH_ASKPASS_SCRIPT=/tmp/ssh-askpass-script
cat > ${SSH_ASKPASS_SCRIPT} <<EOL
#!/bin/bash
echo "${PASS}"
EOL
chmod u+x ${SSH_ASKPASS_SCRIPT}

# Tell SSH to read in the output of the provided script as the password.
# We still have to use setsid to eliminate access to a terminal and thus avoid
# it ignoring this and asking for a password.
export SSH_ASKPASS=${SSH_ASKPASS_SCRIPT}
......
......
# Log in to the remote server and run the above command.
# The use of setsid is a part of the machinations to stop ssh 
# prompting for a password.
setsid ssh ${SSH_OPTIONS} ${USER}@${SERVER} "ls -rlt"
查看更多
beautiful°
5楼-- · 2019-01-21 08:50

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

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-21 08:51

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.

查看更多
Luminary・发光体
7楼-- · 2019-01-21 08:52

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.

查看更多
登录 后发表回答