Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of servers in a .txt file. Assume that the authorized keys are all in place in the servers.

while read server <&3; do #read server names into loop
[[ $server =~ ^[[:space:]]*$ ]] && continue #prevents a empty line in file
    echo "Name read from file - $server"
    #SSH login
    ssh "$server" 
done 3</home/u0146121/Matt/servers.txt

My questions are...

Do I ssh correctly? Will this all be non-interactive?

Note - I would try this out on the servers but I don't have access to them yet and I want to get my syntax right before hand. I am a beginner in bash and ssh.

share|improve this question
Make sure that you don't have "empty" lines in your file, or add [[ $server =~ ^[[:space:]]*$ ]] && continue just below the while line, so as not to run ssh with an empty argument. – gniourf_gniourf Jul 1 '13 at 20:18
Written like that, ssh won't be non-interactive. You need to pipe the commands to the remote shell: for example echo -ne "date\nwhoami\n" | ssh $server will execute the date and whoami commands on $server, display the results locally, then close the connection. For longer scripts, use a "here-doc". – lserni Jul 1 '13 at 20:21
@gniourf_gniourf thanks that's a great suggestion! – mkrouse Jul 1 '13 at 20:26

I think this is what you are looking for

share|improve this answer
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Canadian Luke Jul 1 '13 at 21:12
Hey @Luke :) I was not sure if it was ssh or bash he'd had help with. If it is bash he needed help to, then I believe that he should had put his question on stackoverflow and then I would be able to help him fully. I am sorry if i am wrong :( – Diblo Dk Jul 1 '13 at 21:28
I did not open the link, but the comment I posted is an automatic response. The point of the StackExchange network though is to be the one place to get information, not click here, click there, click up here, down there, etc. – Canadian Luke Jul 2 '13 at 1:56

ssh "$server"

command in this line is interactive. Use something like:

ssh $server "uname -a; date"

it will login to server, execute there uname -a then date, disconnect from server and continue script execution.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.