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
Vipin
  • 4,013
  • 3
  • 28
  • 52
  • 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? – markp-fuso Jun 04 '17 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. – Vipin Jun 04 '17 at 16:03
  • What I meant: replace /bin/bash with /bin/ksh (or the appropriate path to your ksh) and see what happens. – markp-fuso Jun 04 '17 at 16:18
  • Possible duplicate of [Running a command on remote machine without ssh delay](https://stackoverflow.com/questions/34517962/running-a-command-on-remote-machine-without-ssh-delay) – Jakuje Jun 04 '17 at 18:02

2 Answers2

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.

Gilbert
  • 3,364
  • 13
  • 17
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'
Jakuje
  • 20,643
  • 11
  • 53
  • 62
  • 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-fuso Jun 04 '17 at 18:36
  • 1
    @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). – Jakuje Jun 04 '17 at 18:41