1

I'm trying to ssh to 2 servers one by one and run the following command:

sudo node app.js

Here is my code:

#!/bin/bash
while read line; do  
    ssh -i "sshtest.pem" ec2-user@$line "sudo node app.js"     
done < ips.txt

After sudo node app.js is executed the control doesn't pass over to the next ip in the loop. Can someone point me what I could do to improve this?

1 Answer 1

1

Try exiting after your ssh:

#!/bin/bash
COMMAND="sudo node app.js && exit"
PEM="sshtest.pem"
USER="ec2-user"
IN="ips.txt"

while read LINE; do  
    ssh -i $PEM $USER@$LINE $COMMAND     
done < $IN

If you'd like rather not block, background each ssh:

#!/bin/bash
COMMAND="sudo node app.js && exit"
PEM="sshtest.pem"
USER="ec2-user"
IN="ips.txt"

while read LINE; do  
    ssh -i $PEM $USER@$LINE $COMMAND &
done < $IN

The biggest issue you're going to have is with sudo. Having to elevate privilege will require you to enter a password. This might help you re-design your approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.