2

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.

4

3 回答 3

1

我必须了解的第一件事是 $PATH 变量在执行命令时所持有的内容。我跑了

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

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

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

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

于 2012-01-19T22:34:53.617 回答
1

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

stdin, stdout, stderr = server.ssh_client.exec_command("echo $PATH")
print stdout.readlines()
于 2015-06-24T10:27:23.137 回答
0

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

您只有在实际阅读错误消息时才能阅读它,而不是在您发送命令时阅读它。(谢谢你,船长。)

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

于 2012-01-18T20:24:35.057 回答