0

I have a Python (2.7) script that connect to a remote device through SSH and configure it. This script works fine when I run it.

I have a PHP page that execute other Python scripts and they are also working fine. The problem: the SSH script doesn't work when I'm running it from the PHP page. (there is no problem with the PHP page nor the Python script)

To be more specific, the script (when executed by PHP) is running until it gets to the first SSH related code (import paramiko).

How can it be, and is there anything I can do to make it work? Thanks!

(OS: windows XP. Server:Wamp (Apachi http server)

for example, consider this simple code:

fp=open("file.txt","w")
fp.write("text")
import paramiko
fp.write("another text")
fp.close()

if executed by me, everything works fine. If by the PHP page- only the code until "import paramiko" is executed.

or:

fp=open("file.txt","w")
fp.close()
import paramiko
fp2=open("file2.txt","w")
fp2.close()

Executed by me- both files created. by php- only the first.

Moreover: I tried using Try & Catch but nothing comes up. No exception is thrown.

5
  • It'd be especially useful to see the paramiko portion of your python script. Maybe you're using key based authentication, and the key is not accessible to the PHP process? Commented Jun 20, 2013 at 19:40
  • Please, learn to NOT override builtins. While it is possible and language is very flexible, you should not do it when beginning your journey with Python. Commented Jun 20, 2013 at 19:45
  • Nate- How can I make it accessible? Commented Jun 20, 2013 at 19:52
  • 1
    @user2162550 By "override builtins" @Tadeck is talking about your using a file variable. Since file is already defined as type in Python, using that name is bad manners. Commented Jun 20, 2013 at 20:23
  • @cwallenpoole: Yes, it is bad manners, and I meant exactly what you said. There are not many builtins in Python (here is list for 2.7, and here is list for 3.3). It is always worth to not shadow them, so they are available when you (or someone else) needs them. Otherwise you (or someone else) may face the issue with hard-to-debug code. Commented Jun 20, 2013 at 20:28

1 Answer 1

2

My guess is that paramiko is not in your PYTHONPATH environmental variable for the Apache user.

When Apache fires up PHP, it is calling PHP as the whatever the "Apache" user happens to be (in Linux normally apache (or httpd) user in group www-data). Sometimes this user will have different environment variables set than the ones that users normally have. While this generally doesn't matter, sometimes it will cause bizarre interactions (and I can see it happening with Pip).

I would add a try... except around paramiko:

try:
   import paramiko
except Exception as e:
   # log your exception here.
   pass # I put this here so the example can compile.
#rest of the script

That should let you know whether or not it is installed (and I suspect it isn't).

Sign up to request clarification or add additional context in comments.

1 Comment

@user2162550 Could there be a timeout then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.