Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running remote python script from local PHP script

I have two machines, 192.168.10.6 (local machine) which has my php script and 192.168.12.163 (remote machine) which has my python script. How can I run this remote python script from a local PHP script?

I have a working code for running local python script from a local PHP script but I'm not able run remote Python script from a local PHP script.

like image 830
Pavan R Avatar asked Apr 13 '26 11:04

Pavan R


1 Answers

I was about to propose using shell_exec/exec to spawn ssh and run a command on the remote host, for example:

$out = shell_exec('ssh [email protected] "ls -la"');

However, I see that PHP supports that with ssh2_exec, example:

$connection = ssh2_connect('192.168.12.163', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'python /path/to/script');

If ssh2 is not available on your server and you cannot install it, you can try phpseclib (see here for example)

like image 199
urban Avatar answered Apr 16 '26 01:04

urban



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!