Drush 命令不使用 Paramiko 执行

发布于 2024-12-27 15:15:15 字数 1879 浏览 3 评论 0原文

我已按照此处的步骤 http://jessenoller .com/2009/02/05/ssh-programming-with-paramiko-completely- different/

通过 Python 使用 ssh 连接到我的服务器。我可以正常连接并发送命令。

然而,当我运行 stderr.readlines() 时,它每次都会显示下面的错误消息,即使该命令似乎已正确执行。我关闭了连接并重新启动Python,结果仍然相同。

这是一个 Python 示例:

>>> stdin, stdout, stderr = myssh.exec_command("xyz")
>>> stderr.readlines()
['which: no php in (/usr/bin:/bin:/usr/sbin:/sbin:/big/dom/mydomain/pear/drush)\n', '/big/dom/mydomain/pear/drush/drush: line 89: exec: : not found\n', 'bash: xyz: command not found\n']

我安装了 drush,它似乎工作正常。如果我在服务器上输入“which php”,我会被告知它驻留在哪里,而不是上面的错误消息。我发送了一些其他命令来故意获取错误消息,看看它是否清除了任何内容。相反,它在最后附加了一些东西。

根据错误消息,我查看了引用的 drush 文件。这是第 89 行:

exec "$php" $php_options "$SCRIPT_PATH" --php="$php" --php-options="$php_options" "$@"

我相信“which php”命令来自此行上方块中的 $php 变量

if [ ! -z "$DRUSH_PHP" ] ; then
  # Use the DRUSH_PHP environment variable if it is available.
  php="$DRUSH_PHP"
else
  # Default to using the php that we find on the PATH.
  # Note that we need the full path to php here for Dreamhost, which behaves oddly.  See http://drupal.org/node/662926
  php=`which php`

  # We check for a command line (cli) version of php, and if found use that.
  which php-cli >/dev/null 2>&1
  if [ "$?" = 0 ] ; then
    php=`which php-cli`
  fi

  # On MSYSGIT, we need to use "php", not the full path to php
  if [ ! -z "$MSYSTEM" ] && [ "x${MSYSTEM:0:5}" = "xMINGW" ] ; then
    php="php"
  fi
fi

文件的全文位于: http://pastebin.com/29AXmHKF

如果我尝试执行任何 drush 命令,我会收到相同的错误。但是如果我直接登录服务器而不使用 python/paramiko,drush 命令就可以正常工作。

I've followed the steps here http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/

to connect to my server with ssh via Python. I can connect fine and send commands.

When I run stderr.readlines(), however, it shows me the error message below every time, even if the command seems to have executed correctly. I've closed the connection and restarted Python, and still the same result.

Here's a Python sample:

>>> stdin, stdout, stderr = myssh.exec_command("xyz")
>>> stderr.readlines()
['which: no php in (/usr/bin:/bin:/usr/sbin:/sbin:/big/dom/mydomain/pear/drush)\n', '/big/dom/mydomain/pear/drush/drush: line 89: exec: : not found\n', 'bash: xyz: command not found\n']

I have drush installed and it seems to work fine. If I type in "which php" on the server I'm told where it resides, instead of the error message above. I sent some other commands to purposefully get an error message to see if it cleared anything out. Instead it tacked things on at the end.

Following the error message, I went and looked at the drush file referenced. Here's line 89:

exec "$php" $php_options "$SCRIPT_PATH" --php="$php" --php-options="$php_options" "$@"

I believe the "which php" command comes from the $php variable in the chunk above this line

if [ ! -z "$DRUSH_PHP" ] ; then
  # Use the DRUSH_PHP environment variable if it is available.
  php="$DRUSH_PHP"
else
  # Default to using the php that we find on the PATH.
  # Note that we need the full path to php here for Dreamhost, which behaves oddly.  See http://drupal.org/node/662926
  php=`which php`

  # We check for a command line (cli) version of php, and if found use that.
  which php-cli >/dev/null 2>&1
  if [ "$?" = 0 ] ; then
    php=`which php-cli`
  fi

  # On MSYSGIT, we need to use "php", not the full path to php
  if [ ! -z "$MSYSTEM" ] && [ "x${MSYSTEM:0:5}" = "xMINGW" ] ; then
    php="php"
  fi
fi

The full text of the file is here: http://pastebin.com/29AXmHKF

I get the same error if I try to execute any drush command. But drush commands work fine if I just log myself into the server directly w/o using python/paramiko.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾北清歌寒 2025-01-03 15:15:15

我必须了解的第一件事是执行命令时 $PATH 变量保存的内容。我运行

>>> stdin, stdout, stderr = myssh.exec_command("echo $PATH")
>>> stderr.readlines()

并意识到我的 $PATH 与我直接在服务器上运行 echo $PATH 时不同!我只能猜测在通道打开并发送命令后的某个时刻,额外的路径会附加到 $PATH 变量。

然而,$PATH 包含的是 drush 的路径,我之前将其添加到我的主文件夹中的 .bashrc 文件中。因此,我所要做的就是在其中添加 php 的路径(即使当我在服务器上运行“echo $PATH”时该路径就在那里)。

现在我没有收到错误消息,并且可以执行 drush 命令。

The first thing I had to understand is what the $PATH variable held at the time of executing the command. I ran

>>> stdin, stdout, stderr = myssh.exec_command("echo $PATH")
>>> stderr.readlines()

and realized my $PATH was not the same as when I run echo $PATH directly on the server! I can only guess that extra paths get appended to the $PATH variable at some point after the channel is opened and my command is sent.

However, what $PATH did contain was the path to drush which I previously added to the .bashrc file in my home folder. So, all I had to do was also add the path to php there as well (even though that path is there when I run "echo $PATH" on the server).

Now I don't get the error message and I can execute drush commands.

扎心 2025-01-03 15:15:15

我使用了 Mike Ryan 的解决方案(感谢 Mike!),但在 stdout 而不是 stderr 中找到了信息。

stdin, stdout, stderr = server.ssh_client.exec_command("echo $PATH")
print stdout.readlines()

I used Mike Ryan's solution (thanks Mike!) but found the information in stdout, not stderr.

stdin, stdout, stderr = server.ssh_client.exec_command("echo $PATH")
print stdout.readlines()
对风讲故事 2025-01-03 15:15:15

如果您以交互方式 ssh 到该服务器并运行 xyz 会发生什么?

您只能在实际读取错误消息时才能读取错误消息,而不是在发送命令时读取错误消息。 (谢谢队长。)

错误输出看起来非常像您的 xyz 是一个以 #!which php shebang 行开头的 PHP 脚本。但 shell 找不到任何 PHP 可执行文件。这可能是由于登录脚本中未正确设置 PATH 造成的。确保您了解 ssh 到盒子时运行哪个登录脚本(通常是 ~/.bash_profile 和/或 ~/.profile 而不一定是 ~/ .bashrc)。

What happens if you ssh to that server interactively and run xyz?

You only get to read the error message when you actually read it, not when you send the command. (Thank you, Captain.)

The error output looks very much like as if your xyz is a PHP script that starts with a #!which php shebang line. But the shell cannot find any PHP executable. This may be due to PATH not being set correctly in the login script. Make sure you understand which login script runs when you ssh to the box (usually it's ~/.bash_profile and/or ~/.profile and not necessarily ~/.bashrc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文