Linux 如何将参数传递给 SSH 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6690315/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:05:25  来源:igfitidea点击:

How to pass parameters to a SSH command?

linuxsshssh-keys

提问by Zhen

I configure a ssh command key like (in a remote mashine M1):

我配置了一个 ssh 命令键,如(在远程 mashine M1 中):

command="/usr/bin/nslookup",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-dss DEADBEEFDEADBEEF= [email protected]

To allow to run nslookup in M1 from a client machine C1. It works and i can run:

允许从客户端机器 C1 在 M1 中运行 nslookup。它有效,我可以运行:

C1> ssh -i mylookup_key M1

And i get the nslookup execution on M1, but i need to pass parameters to get real work. How can i pass parameters to ssh command key?

我在 M1 上获得了 nslookup 执行,但我需要传递参数才能获得真正的工作。如何将参数传递给 ssh 命令密钥?

p.d: I'm using nslookup as an example of the real program that I want to exec.

pd:我使用 nslookup 作为我想要执行的真实程序的示例。

采纳答案by dogbane

Take a look at the $SSH_ORIGINAL_COMMANDvariable which should contain the command passed to ssh. For example:

查看$SSH_ORIGINAL_COMMAND应该包含传递给 的命令的变量ssh。例如:

command="/usr/bin/nslookup $SSH_ORIGINAL_COMMAND",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-dss DEADBEEFDEADBEEF= [email protected]

Then try:

然后尝试:

$ ssh -i mylookup_key M1 stackoverflow.com

回答by Mihai Maruseac

Have you tried enclosing the command in 'or "? Soemthing like

您是否尝试将命令包含在'或 中"?有点像

ssh host "ls -l -a .."

which gives me

这给了我

total 12
drwxr-xr-x  3 root  root  4096 2011-06-16 12:33 .
drwxr-xr-x 22 root  root  4096 2011-06-16 13:40 ..
drwxr-xr-x 49 mihai mihai 4096 2011-07-14 10:33 mihai

meaning that it works

这意味着它有效

回答by Sodved

If I understand correctly you just want to run a command on the remote machine. The simple case is to just pass the command as the last parameter to ssh

如果我理解正确,您只想在远程机器上运行命令。简单的情况是将命令作为最后一个参数传递给 ssh

ssh -i mylookup_key M1 nslookup stackoverflow.com

But you may need to be careful about quoting and special characters. e.g.

但是您可能需要小心引用和特殊字符。例如

ssh -i mylookup_key M1 ./args.sh "1 2 3"
PROG:./args.sh
ARG[1]: 1
ARG[2]: 2
ARG[3]: 3
ssh -i mylookup_key M1 ./args.sh '"1 2 3"'
PROG:./args.sh
ARG[1]: 1 2 3