AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1131450
Accepted
Milkncookiez
Milkncookiez
Asked: 2023-05-19 00:25:22 +0800 CST2023-05-19 00:25:22 +0800 CST 2023-05-19 00:25:22 +0800 CST

无法通过 ssh 隧道在远程主机上执行嵌套命令

  • 772

我在我的 GitLab CI 作业中运行以下命令:

ssh ${REMOTE_HOST} "docker restart $(docker ps --format '{{.Names}}' | grep '^backend')"

问题是它返回以下错误:

/usr/bin/bash: line 156: docker: command not found
"docker restart" requires at least 1 argument.

我知道它docker确实存在并且可以在远程主机上运行,​​因为我可以登录到主机的 shell 并自己执行 docker 命令。

根据我的理解,错误似乎是双重的——它告诉我“找不到 docker 命令”,然后告诉我“docker restart 至少需要 1 个参数”。

因此,我假设docker找不到嵌套命令的可执行文件,它返回“docker: command not found”错误,并且由于没有返回容器名称,因此也docker restart失败了。

我也尝试在嵌套命令中替换docker为/usr/bin/docker,但无济于事。

我知道 ssh 隧道工作正常,因为就在该命令之前,我执行了以下两个命令,它们工作正常:

ssh ${REMOTE_HOST} "docker pull my_registry/backend:${VERSION} "
ssh ${REMOTE_HOST} "docker-compose -f docker-compose.yml up --detach --remove-orphans"

所以,我真的相信问题是docker找不到内部命令的可执行文件。

ssh
  • 1 1 个回答
  • 26 Views

1 个回答

  • Voted
  1. Best Answer
    Mircea Vutcovici
    2023-05-19T00:34:35+08:002023-05-19T00:34:35+08:00

    双引号字符串仍将允许本地 bash shell 扩展变量、命令替换……在字符串内。

    这意味着您的命令将docker ps --format '{{.Names}}' | grep '^backend'在本地机器上执行,而不是像您希望的那样在远程机器上执行。

    ssh ${REMOTE_HOST} "docker restart $(docker ps --format '{{.Names}}' | grep '^backend')"
    

    您可以尝试使用以下方法转义它:

    ssh ${REMOTE_HOST} "docker restart \$(docker ps --format '{{.Names}}' | grep '^backend')"
    

    或者使用单引号。这是一个例子:

    ssh ${REMOTE_HOST} 'docker restart $(docker ps --format {{.Names}} | grep ^backend)'
    

    更多详细信息,您可以在引用部分找到 bash (1)手册页。

       There are three quoting mechanisms: the escape character, single
       quotes, and double quotes.
    
       A non-quoted backslash (\) is the escape character.  It preserves
       the literal value of the next character that follows, with the
       exception of <newline>.  If a \<newline> pair appears, and the
       backslash is not itself quoted, the \<newline> is treated as a
       line continuation (that is, it is removed from the input stream
       and effectively ignored).
    
       Enclosing characters in single quotes preserves the literal value
       of each character within the quotes.  A single quote may not
       occur between single quotes, even when preceded by a backslash.
    
       Enclosing characters in double quotes preserves the literal value
       of all characters within the quotes, with the exception of $, `,
       \, and, when history expansion is enabled, !.  When the shell is
       in posix mode, the ! has no special meaning within double quotes,
       even when history expansion is enabled.  The characters $ and `
       retain their special meaning within double quotes.  The backslash
       retains its special meaning only when followed by one of the
       following characters: $, `, ", \, or <newline>.  A double quote
       may be quoted within double quotes by preceding it with a
       backslash.  If enabled, history expansion will be performed
       unless an !  appearing in double quotes is escaped using a
       backslash.  The backslash preceding the !  is not removed.
    
    • 3

相关问题

  • 如何最好地设置 ssh 隧道以访问远程网络 (Linux)

  • SSH 和重定向

  • 通过 SSH 会话使用 NET USER 命令拒绝访问

  • SSH 服务器零日漏洞利用 - 保护自己的建议

  • ubuntu apt-get upgrade - 如何在 shell 中单击确定?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve