0

What is nice way to ssh and run multiple commands in korn shell ? I came across related question and top answer works for me but I specify /bin/bash/ in this and running it over korn shell.

Is it ok to run below list of commands (copied from answer of referenced question) or there can be better way for ksh ?

ssh otherhost /bin/bash << EOF
  ls some_folder; 
  ./someaction.sh 'some params'
  pwd
  ./some_other_action 'other params'
EOF
4
  • Have you tried running the example with ksh? (When in doubt, try it out!); if you received an error ... provide your complete example and error; what's your idea of 'better' and what are you trying to accomplish that this example (or any of the other examples at that link) doesn't solve? Commented Jun 4, 2017 at 15:50
  • @markp As communicated in question yes I tried and it is running fine as well, so no error. In command I am using /bin/bash/ but I am in korn shell, that's why I am asking if there any other way probably better using ksh and not using bash. Commented Jun 4, 2017 at 16:03
  • What I meant: replace /bin/bash with /bin/ksh (or the appropriate path to your ksh) and see what happens. Commented Jun 4, 2017 at 16:18
  • Possible duplicate of Running a command on remote machine without ssh delay Commented Jun 4, 2017 at 18:02

2 Answers 2

2

Yes, that works. But if later commands can do bad things if prior commands failed you had better stop the chain if prior commands fail.

One simple way is to chain the commands together with &&

  A && B && { C || true $? } && D

Or,

  A && 
  B &&
  { C || true $? } && 
  D

The trick on C allows that command to fail but keep the chain going... grep often needs this to continue if nothing matches. The argument of $? on true does nothing, unless running under set -x, then it will trace the error condition of C.

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

Comments

0

It does not matter what shell you are using. I would rather stick with the solution provided in one of the answers, that recommended using connection multiplexing.

What is it? You will run separate ssh commands, but all of them will run in a single TCP connection.

And how to achieve that? Create a configuration file in ~/.ssh/config with similar content:

Host otherhost
  ControlPath ~/.ssh/controlmasters/%r@%h:%p
  ControlMaster auto
  ControlPersist yes

And then run the commands as you would be running locally (with ssh server prefixed). The first will initiate the connection, the other will just use it

ssh otherhost ls some_folder; 
ssh otherhost ./someaction.sh 'some params'
ssh otherhost pwd
ssh otherhost ./some_other_action 'other params'

2 Comments

One issue with using ssh/multiplexing (assuming single command per ssh call - as per your example) is that successive commands cannot rely on results of previous commands (ie, each call starts (over) in home directory); simple example: cd /tmp ; pwd ... when submitted as 2 lines in a HERE document then pwd returns /tmp ... when submitted as 2 separate ssh/cmd calls then pwd returns /remote/home/directory; point being that the solution (HERE document; ssh/multiplexing; something else) has to consider the desired set of actions and whether or not they can stand on their own
@markp Obviously, this can be a drawback, but it can be also and advantage that one can not mess up the environment for the other. Clearly, this requirement for the dependence between the commands was not stated and therefore I am offering this state of the art solution as an option to the ugly bash redirection with HERE document and other proposed solutions in the referenced question, which limit the usability (in name of interaction with the script, other redirection and so on).

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.