通过 Plink 在 Windows 命令行上运行 shell 脚本(带参数) [英] Run shell script (with parameters) on Windows command line via Plink

查看:26
本文介绍了通过 Plink 在 Windows 命令行上运行 shell 脚本(带参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Windows 的 Linux 机器内远程执行一个 shell 脚本

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"

这是我从 Windows 命令提示符运行的命令

Here is the command I ran from Windows command prompt

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

我得到的输出为

非法的参数数量"

有什么方法可以将命令行参数传递给将在远程服务器上执行的 shell 脚本吗?

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

推荐答案

你误解了 -m 开关的工作原理.

You misunderstand how the -m switch works.

这只是一种让 plink 加载命令从本地文件发送到服务器的方法.

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.

解决方法是在从批处理文件(例如 run.bat)运行 plink 之前在本地动态生成文件:

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

以上将使脚本在服务器上执行echo 5.

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

如果脚本很复杂,不要在本地组装它,而是在服务器上准备好它(如@MarcelKuiper 建议的那样)并通过 Plink 只执行脚本.

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"

在这种情况下,由于我们只执行一个命令,您可以在 Plink 命令行上传递它,包括参数.您不必对(临时)文件使用 -m 开关.

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 在 Windows 命令行上运行 shell 脚本(带参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆