简体   繁体   English

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

[英]How to pass parameters to a SSH command?

I configure a ssh command key like (in a remote mashine M1):我配置了一个 ssh 命令键,例如(在远程机器 M1 中):

command="/usr/bin/nslookup",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-dss DEADBEEFDEADBEEF= mycow@farm.org

To allow to run nslookup in M1 from a client machine C1.允许从客户端机器 C1 在 M1 中运行 nslookup。 It works and i can run:它有效,我可以运行:

C1> ssh -i mylookup_key M1

And i get the nslookup execution on M1, but i need to pass parameters to get real work.我在 M1 上执行 nslookup,但我需要传递参数才能真正工作。 How can i pass parameters to ssh command key?如何将参数传递给 ssh 命令键?

pd: I'm using nslookup as an example of the real program that I want to exec. pd:我使用 nslookup 作为我想要执行的真实程序的示例。

Take a look at the $SSH_ORIGINAL_COMMAND variable which should contain the command passed to ssh .查看$SSH_ORIGINAL_COMMAND变量,它应该包含传递给ssh的命令。 For example:例如:

command="/usr/bin/nslookup $SSH_ORIGINAL_COMMAND",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-dss DEADBEEFDEADBEEF= mycow@farm.org

Then try:然后尝试:

$ ssh -i mylookup_key M1 stackoverflow.com

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意味着它有效

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.但是您可能需要小心引用和特殊字符。 eg例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM