简体   繁体   中英

How to run script on 200 servers and print output in bastion instance terminal? My bash script hangs on ssh to remote server

Our servers are on AWS (ec2) and I have to run same set of commands on 200 servers and need ouput on my terminal. I have bastion instance which can access all 200 servers. This question is initiallly being asked in 2015. I have a script which was running fine on rackspace but on AWS, default user is ec2-user so I need to adjust script as per ec2-user or switch to root user during the ssh.

Is there any syntax issue or something missing? My script name is runOnAppServer.sh and then file having all ips then commands file as mentioned --> runOnAppServer.sh server_ip scriptFile

Script is mentioned below

#!/bin/bash
# The private key used to identify this machine

IDENTITY_KEY=/home/sshKey.pem

syntax()
{
    echo "Syntax: runOnAppServer.sh server_ip scriptFile]"
    echo "For example: ./runOnAppServer.sh server_ip scriptFile"
    exit 1
}

if [ $# -ne 2 ]
then
    echo not enough arguments
    syntax
fi

echo "Running script $2 on $1"
ssh -t -t  -i $IDENTITY_KEY ec2-user@$1 sudo -i 'bash -s' < $2
exit
EOT

echo "Done"

Because that's what your script does. It runs sudo -i 'bash -s' with no arguments over ssh , which causes the remote Bash to start an interactive shell, and wait for your input. You are instructing ssh to read input from $2 , but it doesn't work that way.

The exit and EOT are technically syntax errors, yes. It appears that you hope the ssh command to somehow read the following lines of the script it is being run from, but it doesn't work that way, either.

You appear to be looking for something like Execute local script on remote Linux host

This is not duplicate Question or Problem that I discussed

I don't want to use scp, I wanted to avoid scp to copy the file to remote server then execute, I am directly executing commands from my server using bash script to remote server, by this way, I was running commands on 100 of servers, and on that time there was no proper tool which does this. Now AWS is offering you SSM etc.

Answer:

The reason my script hangs while running a script was, I was using sudo -i and 
then 'bash -s', so what I need to do is, I just need to remove sudo -i 'bash -s' 
and add sudo in front of all commands that I am try to pass using file $2 from 
outside the file using another file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM