2

I have about 100 remote servers. I want to manage all my servers using one terminal server to update , run commands with sudo privileges.

I need to have one main script that takes another test script as an argument. The main script run through a with loop using a hosts file with remote server names.

The test script has all the actual sudo commands to be run on the remote servers at once

./mainscript hostfile testscript for example, test script may have "sudo yum -y update" or a string of commands.

This seems easy if I login as root user. But I want to run this as user 'admin' who has sudo privileges on all the remote servers. All these scripts are located locally on terminal server.

Please suggest me any ideas you may have to achieve this?

7

5 Answers 5

3

One idea, though it might not be the best out there, is for you to configure ssh to use key based authentication instead of password authentication. This instructions should be sufficient in case you are not aware of it SSH Key Authentication

I don't know what your test script contains nor the complexity of it. let's assume that it is a fair complex script and it saves the output under /tmp/script_output/ and in this case I suggest the following:

while read -r i
do
   scp $2 admin@remote_server:/tmp/
   ssh -t admin@remote_server /tmp/$2
   ssh admin@remote_server rm /tmp/$2
   scp -r admin@remote_server:/tmp/script_output/ /tmp/
   ssh admin@remote_server rm -rf /tmp/script_output
done < "$1"

By the way, from the sudoers

#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
#         You have to run "ssh -t hostname sudo <cmd>".
#
Defaults    requiretty
4
  • 3
    I seriously doubt anyone wants to rm -rf /tmp.
    – jordanm
    Commented Jan 8, 2013 at 19:55
  • This is bad, it needs to setup a SSH session five times which takes quite some time for 100 servers. You'd better combine the commands like ssh admin@server 'cat > file && chmod +x file && ./file;rm -f file' < script >output or something like that (for chaining). Also, this answer does not answer the question with sudo.
    – Lekensteyn
    Commented Jan 8, 2013 at 20:32
  • Hello all, I am using key based authentication for all the logins. But I still need to know how to use sudo in this ssh command. I am using ssh -tt admin@server , it does what it need to do ,but spits out an error "tcgetattr: Inappropriate ioctl for device" . For time being , I am redirecting the error to a log error file. Also, Lekensteyn, do you think the following commmand will be helpful instead of what alexandre suggested. ssh admin@server 'cat > file && chmod +x file && ./file;rm -f file' < testscript >output
    – Shashi
    Commented Jan 8, 2013 at 20:56
  • Answering all, Corrected the rm -rf /tmp as I what I wanted was clear the script output. Add the -t to the ssh as per sudoers specification. The sudo requires tty.
    – BitsOfNix
    Commented Jan 9, 2013 at 8:12
1

I had the same problem some time ago. That's my solution:
https://github.com/maciejkorzen/misc-ruby/blob/master/ssh-batch-upload-sftp-and-run.rb

  • Download this script.
  • Create CSV file with names of servers and passwords (I assume that login is the same on each server).
  • Create script that you want to execute.
  • Adjust variables in my Ruby script, run it and let it do all the work for you. :-)
1

The following will allow you to execute sudo commands on multiple hosts while only having to enter your sudo password once at the beginning.

What is missing is the for loop or something that sets the HOSTS variable to the hosts you have. The script creates a file in the temp folder that contains the sudo password, but that file is deleted immediately. This allows the command to use the sudo password without it being displayed in the terminal window or in your script.

#!/bin/bash

if "something" 
 then

 else

cat > /tmp/$HOSTS-pw.sh <<EOS
#!/bin/sh
ssh user@$HOSTS sudo "your command here" <<EOC
$SUDOPW
EOC
EOS

fi

chmod 700 /tmp/$HOSTS-pw.sh
/tmp/$HOSTS-pw.sh >/dev/null
if [ -f /tmp/$HOSTS-pw.sh ]; then rm -f /tmp/$HOSTS-pw.sh; fi

echo "Enter SUDO password:"
read -s SUDOPW

for loop here!

unset SUDOPW
exit 0
0

Strange way to do this.

I do some things at many servers using clusterssh (type once, do it on every server).

For bigger changes we use our own repositories that are activated on every server. That way we use the OS patch-mechanism to distribute specific changes to a large number of servers.

0

For such a scenario I have a two scripts. The script runonnodes does all the ssh to the selected serves (a list of servers). The other one named runonnodes_commands is on a network share, which is available on each server. This is the script executed on each selected server.

The first script runonnodes includes mainly:

#!/bin/bash

SELECTEDNODES="node1 node2 node2"

for NODE in $SELECTEDNODES; do
    ssh -t $NODE "sudo su -c /mnt/share/runonnodes_commands"

For this to work, key based authentication is used (my public key in ~/.ssh/authorized_keys) and I am allowed to do sudo su

The script runonnodes_commands contains all the stuff, which has to be done on the nodes, e.g. copy files, install packages:

#!/bin/bash

yum install python

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .