Drush commands not executing using Paramiko

流过昼夜 提交于 2019-12-11 01:58:29

问题


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.


回答1:


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.




回答2:


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()



回答3:


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).



来源:https://stackoverflow.com/questions/8916650/drush-commands-not-executing-using-paramiko

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!