bash 通过 Plink 在 Windows 命令行上运行 shell 脚本(带参数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27462589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run shell script (with parameters) on Windows command line via Plink
提问by aaditya 1985
I need to execute a shell script remotely inside the Linux box from Windows
我需要从 Windows 在 Linux 机器内远程执行一个 shell 脚本
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit
fi
echo ""
Here is the command I ran from Windows command prompt
这是我从 Windows 命令提示符运行的命令
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?
有什么方法可以将命令行参数传递给将在远程服务器上执行的 shell 脚本?
回答by Martin Prikryl
You misunderstand how the -m
switch works.
你误解了-m
开关的工作原理。
It is just a way to make plink
load the commands to send to the server from a local file.
这只是一种plink
加载命令以从本地文件发送到服务器的方法。
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
):
一种解决方法是在plink
从批处理文件运行之前在本地动态生成文件(例如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.
以上将使脚本echo 5
在服务器上执行。
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.
如果脚本很复杂,不要在本地组装它,而是在服务器上准备好它(如@MarcelKuiper 建议的那样)并通过 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.
在这种情况下,由于我们只执行一个命令,您可以在 Plink 命令行上传递它,包括参数。您不必将-m
开关与(临时)文件一起使用。
回答by Alok Tiwari
I triggered the Shell script in "commands.txt" from Plink which worked for me like a charm with below method I tried:
我在 Plink 的“commands.txt”中触发了 Shell 脚本,它对我有用,我尝试了以下方法:
- 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.
注意:在引号中使用第一个 EOF,例如 <<'EOF',而不是最后一个。否则你会看到你的代码会表现得很奇怪。请参阅下文。
Example:
例子:
sudo -i <<'EOF'
<your script here>
EOF
Then, finally run it using Plink:
然后,最后使用 Plink 运行它:
plink -ssh username@hostname -pw password -m commands.txt
回答by Marcel Kuiper
Have you tried putting the command and argument in quotes:
您是否尝试将命令和参数放在引号中:
i.e. -m "C:\myscript.sh 5"
即 -m "C:\myscript.sh 5"