1

I want to ssh to a node and run a command there and then exit. This is repeated for all nods. The script is fairly simple

#!/bin/bash
NODES="compute-0-0 compute-0-1 compute-0-2 compute-0-3"
for i in $NODES
do
   ssh $i
   ls -l /share/apps 
   rpm -ivh /share/apps/file.rpm
   exit
done

But the problem is that, after the ssh, the ls -l command is missed. Therefore, the command prompt waits for an input!

Any way to fix that?

UPDATE:

I modified the loop body as

ssh $i <<END
 ls -l /share/apps
 exit
END

But I get

./lst.sh: line 9: warning: here-document at line 5 delimited by end-of-file (wanted `END')
./lst.sh: line 10: syntax error: unexpected end of file
3
  • 2
    See stackoverflow.com/questions/37586811/… Commented Jul 7, 2016 at 7:40
  • If you want your ssh section of the script to be indented, use <<-END Commented Jul 7, 2016 at 7:54
  • 1
    The answer you accepted should work. However, your here-doc approach was not wrong either. I think, you have added some spaces before END after the exit line. Alternately, if you wnt those spaces for indentation, change the heredoc line to ssh $i <<-END (Notice the <<- operator instead of <<) Commented Jul 7, 2016 at 7:54

3 Answers 3

3

Try this

    #!/bin/bash
    NODES="compute-0-0 compute-0-1 compute-0-2 compute-0-3"
    for i in $NODES
    do
       ssh $i "ls -l /share/apps;rpm -ivh /share/apps/file.rpm;exit;"
    done
Sign up to request clarification or add additional context in comments.

1 Comment

NODES should be an array, not a string. nodes=( compute-0-{0,1,2,3} ), then for i in "${nodes[@]}"; do -- that way you're not depending on string-splitting on IFS.
1

I'd change the script and would run the ssh command with the the command to execute.

For example:

#!/bin/bash
NODES="compute-0-0 compute-0-1 compute-0-2 compute-0-3"
for i in $NODES
do
   ssh $i "ls -l /share/apps && rpm -ivh /share/apps/file.rpm && exit"
done

The && operator means that each command will be executed only if the previous command succeeded.

If you want to run the command independently, you can change the && operator to ; instead.

Comments

1

identation is everything in this type of scripts, one sample bash script for sshing into different servers and exec actions on them:

#!/bin/bash
NODES="[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]"
for i in $NODES
do
  echo "SSHing..."
  echo $i
ssh $i << EOF
  cd /home/user/server.com/current/
  bundle exec eye stop all
  echo "Some Process Stopped Successfully!"
EOF

done

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.