首页 > 解决方案 > 为什么当我尝试远程执行命令时出现错误?

问题描述

我有一个关于通过 SSH 远程执行命令的问题。我在下面尝试。

ssh xx.xx.xx.xx "psql -U qradar -c "select count(id) from offense_view where to_timestamp(start_time/1000) > NOW() - interval '180 minutes'"

它给出了一个错误,如:

Pseudo-terminal will not be allocated because stdin is not a terminal.
ERROR:  syntax error at or near "180"
LINE 1: ... to_timestamp(start_time/1000) > NOW() - interval 180 minute...

标签: linuxbashshellquoting

解决方案


问题是您使用双引号来分隔命令内部的参数ssh和参数psql。这会导致您的字符串被错误地解析。您还缺少psql命令的结尾双引号。

在 shell 中嵌套引号很棘手,使用ssh. 如果您使用 here-doc 会更容易。

ssh xx.xx.xx.xx <<EOF
psql -U qradar -c "select count(id) from offense_view where to_timestamp(start_time/1000) > NOW() - interval '180 minutes'"
EOF

推荐阅读