如何在单个 ssh 命令中使用 bash $(awk)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14707307/
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 use bash $(awk) in single ssh-command?
提问by Redsandro
I am trying to execute a single command over ssh
that contains `sub-code` or $(sub-code) (I use it all the time but I don't know the official name for that) to be executed first, but on the target server.
我正在尝试执行一个ssh
包含 `sub-code` 或 $(sub-code)(我一直使用它,但我不知道它的正式名称)的单个命令,该命令首先被执行,但是在目标服务器。
For the sake of argument, let's say this is the command I want to use. Ofcourse this can be done with hostname
, but this is just a simplified example that contains all the formatting wizardry I want to use.
为了论证起见,假设这是我要使用的命令。当然,这可以用 来完成hostname
,但这只是一个包含我想要使用的所有格式化向导的简化示例。
echo `uname -a | awk '{print }'`
No problem. But how do you escape this properly to send it over ssh
in a single command? The following is wrong, because it lets the server reply your local hostname. The sub-code is executed locally:
没问题。但是你如何正确地转义它以ssh
在单个命令中发送它?以下是错误的,因为它让服务器回复您的本地主机名。子代码在本地执行:
ssh myServer echo `uname -a | awk '{print }'`
But any escape-ishness that comes to mind doesn't work:
但是任何想到的逃避都行不通:
$ ssh myServer echo \`uname -a | awk '{print }'\`
awk: cmd. line:1: {print }`
awk: cmd. line:1: ^ invalid char '`' in expression
$ ssh myServer echo $(uname -a | awk '{print }')
bash: syntax error near unexpected token `('
$ ssh myServer echo $\(uname -a | awk '{print }')
bash: syntax error near unexpected token `)'
$ ssh myServer echo $\(uname -a | awk '{print }'\)
awk: cmd. line:1: {print })
awk: cmd. line:1: ^ syntax error
bash: -c: line 0: unexpected EOF while looking for matching `)'
bash: -c: line 1: syntax error: unexpected end of file
I would like an answer that includes using properly escaped ` or $() because I'd like to know if it's possible.
echo` could be something more complicated.
我想要一个包括使用正确转义的` or $() because I'd like to know if it's possible.
echo`的答案可能会更复杂。
回答by user000001
Try this
尝试这个
ssh myServer "uname -a | awk '{print $2}' "
Use the double quotes "
in order to group the commands you will execute remotely.
使用双引号"
将您将远程执行的命令分组。
You also need to escape the $
in the $2
argument, so that it is not calculated locally, but passed on to awk
remotely.
您还需要对参数$
中的进行转义$2
,这样它就不会在本地计算,而是传递到awk
远程。
Edit:
编辑:
If you want to include the $(
)
, you again have to escape the $
symbol, like this:
如果要包含$(
)
,则必须再次对$
符号进行转义,如下所示:
ssh myServer "echo $(uname -a | awk '{print $2}') "
You can also escape the backtick`, like this:
您还可以转义反引号`,如下所示:
ssh myServer "echo \`uname -a | awk '{print $2}'\` "
回答by Davide Berra
What about piping the output of the command and run awk locally?
管道命令的输出并在本地运行 awk 怎么样?
ssh yourhost uname -a | awk '{ print } '
回答by anubhava
It is better to use a heredocwith ssh
to avoid escaping quotes everywhere in a more complex command:
最好使用heredocwithssh
以避免在更复杂的命令中到处转义引号:
ssh -T myServer <<-'EOF'
uname -a | awk '{print }'
exit
EOF
回答by AlecTMH
The workaround is to create an sh file with the command then pass it to ssh command:
解决方法是使用该命令创建一个 sh 文件,然后将其传递给 ssh 命令:
test.sh
测试文件
echo `uname -a | awk '{print }'`
and command:
并命令:
ssh myServer 'bash -s' < test.sh
UPDATE:
更新:
The command without quotes works fine as well:
不带引号的命令也可以正常工作:
ssh myServer uname -a | awk '{print }'
回答by user2383357
If you are trying to get the hostname, use:
如果您尝试获取主机名,请使用:
ssh myServer "uname -n"
ssh myServer "uname -n"
回答by Kangqiao Zhao
Just some additional note:
只是一些补充说明:
In my case, the escape of the $
doesn't help:
就我而言,逃逸$
无济于事:
ts1350:> ssh sysadm@node "grep feature features.txt| awk '{print }' "
Password:
A feature -name ue_trace_mme | off
ts1350:> ssh sysadm@node "grep feature features.txt| awk '{print $6}' "
Password:
awk: cmd. line:1: {print \}
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: {print \}
awk: cmd. line:1: ^ syntax error
ts1350:> ssh sysadm@node "grep feature features.txt| awk '{print \}' "
Password:
awk: cmd. line:1: {print \}
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: {print \}
awk: cmd. line:1: ^ syntax error
While add a space
between $
and the number works:
虽然添加一个space
介于$
和数字之间的作品:
Fri Oct 4 14:49:28 CEST 2019
ts1350:> ssh sysadm@node "grep feature features.txt| awk '{print $ 6}' "
Password:
off
Hope this secodnary way help someone like me.
希望这种次要方式可以帮助像我这样的人。
Thanks to @dave in his answer: How to print a specific column with awk on a remote ssh session?
感谢@dave 在他的回答中:How to print a specific column with awk on a remote ssh session?