简体   繁体   中英

execution of ssh in python script which is called from php exec command

I have a PHP code which calls a python script.In python script i am connecting to a remote system using ssh and running a command.When python code is executed independently all seems perfect,but when i call it from php, the connection can't be established and it prints an empty string as output in the browser.

Here is my php script

<?php
$command = escapeshellcmd('python p.py');
$output = shell_exec($command);
echo $output;
?>

Python script(p.py) is as follows

import subprocess

proc = subprocess.Popen(["ssh remotehost uptime"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "program output:", out

When I execute

python p.py

the program successfully prints the result.And when the php script is executed the browser just displays

program output:

which shows python script gets executed and the problem is in ssh connection when called through php. can anyone suggest me a way to make it work properly?

EDIT:

when my python code has database connections ,similar issues araised . After surfing i found that in /etc/sysconfig I have to give

setsebool -P httpd_can_network_connect_db=1

Is there any similar issues that should be considered for connections over ssh??

Try using this code in your php code. Note : the file should have a .php extension.

 <html>  
 <head></head>  
 <body>  
 <H3>Executing...</H3>  
 <?php exec("p.py"); ?>  
 <H3>Completed...</H3>  
 </body>  
 </html>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM