1

I'm trying to run SSH in python through os.system() which is being invoked by PHP. It works fine when I run it through the CLI version of PHP but it is not running through lamp.

Following is my php code:

<?php $x=shell_exec('python test.py'); ?>

and below is the python code (test.py) which is being invoked by the PHP code:

import os
os.system("ssh [email protected]")

I also tried using touch utility through exec() in PHP, the same thing happens.

7
  • Does it even get to the Python script? Can PHP find the Python interpreter? Is shell_exec() and exec() allowed per your configuration? You could try to debug by putting a print statement in the shell_exec() or exec() calls (or write to file if you want); if that works try a print statement in your python script and work from there. Commented May 15, 2015 at 7:05
  • @SaeX Yes, the php script is able to execute python, there were print statements in my python code, which were printing. exec() is able to execute commands like ls and ps on my system. Commented May 15, 2015 at 7:17
  • It may be running the ssh command with a different user than you're doing it from the CLI. Maybe the user running PHP doesn't have the servers key in its key file or something. Commented May 15, 2015 at 7:27
  • @RameshDahiya : Is it possible to run php as root? Commented May 15, 2015 at 7:29
  • Never do that. Its a suicide. Its always better to use built in functions. I dont know what you trying to accomplish using python script. But would suggest to use PHP inbuilt fucntions php.net/manual/en/function.ssh2-connect.php. Commented May 15, 2015 at 7:36

1 Answer 1

1

Use phpseclib. Using the ssh2 extension as recommended by @Ramesh Dahiya is bad for a number of reasons as elaborated here:

http://phpseclib.sourceforge.net/ssh/compare.html

Example of phpseclib being used:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.