Linux 如何使用参数通过 ssh 执行远程命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18502945/
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
How to execute a remote command over ssh with arguments?
提问by Alex
In my .bashrc
I define a function which I can use on the command line later:
在我的.bashrc
我定义了一个函数,稍后我可以在命令行上使用它:
function mycommand() {
ssh [email protected] cd testdir;./test.sh ""
}
When using this command, just the cd
command is executed on the remote host; the test.sh
command is executed on the local host. This is because the semicolon separates two different commands: the ssh
command and the test.sh
command.
使用该命令时,只cd
在远程主机上执行该命令;该test.sh
命令在本地主机上执行。这是因为分号分隔了两个不同的命令:ssh
命令和test.sh
命令。
I tried defining the function as follows (note the single quotes):
我尝试如下定义函数(注意单引号):
function mycommand() {
ssh [email protected] 'cd testdir;./test.sh ""'
}
I tried to keep the cd
command and the test.sh
command together, but the argument $1
is not resolved, independent of what I give to the function. It is always tried to execute a command
我试图将cd
命令和test.sh
命令放在一起,但参数$1
没有解决,与我给函数的内容无关。总是尝试执行命令
./test.sh
on the remote host.
在远程主机上。
How do I properly define mycommand
, so the script test.sh
is executed on the remote host after changing into the directory testdir
, with the ability to pass on the argument given to mycommand
to test.sh
?
如何正确定义mycommand
,所以剧本test.sh
正在改变进入目录后,在远程主机上执行testdir
,具有传递给参数的能力mycommand
来test.sh
?
采纳答案by konsolebox
Do it this way instead:
改为这样做:
function mycommand {
ssh [email protected] "cd testdir;./test.sh \"\""
}
You still have to pass the whole command as a single string, yet in that single string you need to have $1
expanded before it is sent to ssh so you need to use ""
for it.
您仍然必须将整个命令作为单个字符串传递,但在该单个字符串中,您需要$1
在将其发送到 ssh 之前对其进行扩展,因此您需要使用""
它。
Update
更新
Another proper way to do this actually is to use printf %q
to properly quote the argument. This would make the argument safe to parse even if it has spaces, single quotes, double quotes, or any other character that may have a special meaning to the shell:
执行此操作的另一种正确方法实际上是使用printf %q
正确引用参数。这将使参数可以安全地解析,即使它有空格、单引号、双引号或任何其他可能对 shell 具有特殊含义的字符:
function mycommand {
printf -v __ %q ""
ssh [email protected] "cd testdir;./test.sh $__"
}
- When declaring a function with
function
,()
is not necessary. - Don't comment back about it just because you're a POSIXist.
- 使用 , 声明函数时
function
,()
不是必需的。 - 不要仅仅因为您是 POSIXist 就对此发表评论。
回答by Ted Bigham
Reviving an old thread, but this pretty clean approach was not listed.
恢复旧线程,但未列出这种非常干净的方法。
function mycommand() {
ssh [email protected] <<+
cd testdir;./test.sh ""
+
}
回答by Cyril Duchon-Doris
This is an example that works on the AWS Cloud. The scenario is that some machine that booted from autoscaling needs to perform some action on another server, passing the newly spawned instance DNS via SSH
这是一个适用于 AWS 云的示例。场景是一些从自动缩放启动的机器需要在另一台服务器上执行一些操作,通过 SSH 传递新生成的实例 DNS
# Get the public DNS of the current machine (AWS specific)
MY_DNS=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
ssh \
-o StrictHostKeyChecking=no \
-i ~/.ssh/id_rsa \
[email protected] \
<< EOF
cd ~/
echo "Hey I was just SSHed by ${MY_DNS}"
run_other_commands
# Newline is important before final EOF!
EOF
回答by muratgozel
I'm using the following to execute commands on the remote from my local computer:
我正在使用以下命令从本地计算机远程执行命令:
ssh -i ~/.ssh/$GIT_PRIVKEY user@$IP "bash -s" < localpath/script.sh $arg1 $arg2