7

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

#!/bin/bash
if [ "$#" -ne 1 ]; then

    echo "Illegal number of parameters"
    exit
fi
    echo "$1"

Here is the command I ran from Windows command prompt

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

I am getting output as

"Illegal number of parameters"

Is there any way I can pass command line parameter to shell script which will execute on remote server?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
aaditya 1985
  • 71
  • 1
  • 1
  • 3

3 Answers3

10

You misunderstand how the -m switch works.

It is just a way to make plink load the commands to send to the server from a local file.

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

It's contents is read locally and sent to the server and executed there as if you typed it on a (remote) command line. You cannot give it arguments.


A workaround is to generate the file on the fly locally before running plink from a 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, instead of assembling it locally, have it ready on the server (as @MarcelKuiper suggested) and execute just the script via Plink.

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

In this case, as we execute just one command, you can pass it on Plink command line, including the arguments. You do not have to use the -m switch with a (temporary) file.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

I triggered the Shell script in "commands.txt" from Plink which worked for me like a charm with below method I tried:

  • You can define your script as an one liner using && in a file (I defined in one liner)
  • You need to run your command in <

Note: Use first EOF in quote like <<'EOF' but not the last one. Else you will see you code will behave weirdly. Please see below.

Example:

sudo -i <<'EOF'
<your script here>
EOF

Then, finally run it using Plink:

plink -ssh username@hostname -pw password -m commands.txt
Alok Tiwari
  • 354
  • 3
  • 8
  • Do not use `echo y` to blindly accept a host key. You are losing security. – Martin Prikryl Dec 25 '19 at 10:06
  • Correct, I had to do this which helped me disable all interactive prompts + accepting ssh hostkeys. I would recommend to use -batch if it's only disable all interactive prompts. plink -batch username@server-IP -pw password -m comments.txt – Alok Tiwari Dec 25 '19 at 12:48
  • `echo y` does not *"disable all interactive prompts"*, what's what `-batch does`. – Martin Prikryl Dec 25 '19 at 20:22
  • I also have same understanding. But it is somehow working without giving -batch since I started using echo y. – Alok Tiwari Dec 26 '19 at 03:34
  • @MartinPrikryl I have edited the same to plink -ssh username@hostname -pw password -m commands.txt which should be fine now. – Alok Tiwari Dec 26 '19 at 12:57
0

Have you tried putting the command and argument in quotes:

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

  • Ya i put i got error unable to open command file "C:\myscript.sh 5" – aaditya 1985 Dec 13 '14 at 19:27
  • mmm, i downloaded it and played around. It looks like it plays your batchfile as-is. If you want to use arguments you should have that shellscript already at the other side and then call without the -m option – Marcel Kuiper Dec 13 '14 at 20:22