Run the shell script (with parameters) on the Windows command line via Plink

I need to execute a shell script remotely inside a Linux window from Windows

#!/bin/bash if [ "$#" -ne 1 ]; then echo "Illegal number of parameters" exit fi echo "$1" 

Here is the command that I ran from the windows command prompt

  cmd> plink.exe -ssh username@host -pw gbG32s4D/ -m C:\myscript.sh 5 

I get output as

"Invalid number of parameters"

Is it possible to pass a command line parameter to a shell script that will be executed on a remote server?

+7
source share
2 answers

You misunderstand how the -m switch works.

This is just a way to get plink to load commands to send to the server from a local file.

The file is NOT downloaded and executed on the remote server (with arguments).

The content is read locally and sent to the server and executed there as if you entered it in a (remote) command line. You cannot give him arguments.


The workaround is to generate the file "on the fly" locally before running plink from the batch file (say run.bat ):

 echo echo %1 > script.tmp plink.exe -ssh username@host -pw gbG32s4D/ -m script.tmp 

Then run the batch file with the argument:

 run.bat 5 

The above will make the script execute echo 5 on the server.


If the script is complex, rather than building it locally, prepare it on the server (as @MarcelKuiper suggested) and only run the script through Plink.

 plink.exe -ssh username@host -pw gbG32s4D/ "./myscript.sh %1" 

In this case, when we execute only one command, you can pass it on the Plink command line, including arguments. You do not need to use the -m switch with a temporary file.

+10
source

You tried to put the command and argument in quotation marks:

i.e. -m "C: \ myscript.sh 5"

0
source

Source: https://habr.com/ru/post/979547/


All Articles