Home > OS >  Executing Commands from Windows on Unix via Batchfile
Executing Commands from Windows on Unix via Batchfile

Time:05-11

I have a problem that cannot find a solution for quite a while. I want to execute following line from a Batch file on my windows machine:

ssh %1@%2 "D: && ssh %3@%4 cd /media/usbmsd/ && cp "$(ls -t /media/usbmsd | head -1)" /buffer"

THis batch file will be later executed from a cmd line with the parameters. I am trying to access one system (windows) via ssh and that hop again via ssh to the another system(unix) and there I need to find the newest file in the /media/usbmsd directory and copy it to the folder buffer.

When I excetuting it from my cmd line i am getting following error:

'head' is not recognized as an internal or external command, operable program or batch file.

I have to say that I am not very experienced with this kind of application and am happy about any help

Greeting Denis

CodePudding user response:

You could connect via a proxy jump, then you don't need a second ssh command.
Then a bit obscure escaping of the quotes is necessary.
(I tested this without the intermediate windows client) The first caret ^ is necessary to quote from the cp command, the backslashes are necessary to convince the first expansion to leave the quotes untouched.

ssh %3@%4 -o ProxyJump %1@%2 ^"cd /media/usbmsd/ && cp \"$(ls -t /media/usbmsd | head -1)\" /buffer'

But it should be much easier to place a script on the destination host mycopyscript.sh

cd /media/usbmsd/ && cp "$(ls -t /media/usbmsd | head -1)" /buffer

And then use:

ssh %3@%4 -o ProxyJump %1@%2 "./mycopyscript.sh"
  • Related